Dodawanie paddingu na widoku programowo

[7]}jestem deveoping Android v2. 2 app.

Mam fragment.

W wywołaniu onCreateView(...) mojej klasy fragment nadmuchuję layout do fragmentu jak poniżej:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.login, null);

    return view;
}

Powyższy plik z layoutem to (login.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
 >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Username" />


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Username" />

</LinearLayout>

Chciałbym ustawić paddingTop na powyższy element <LinearLayout>i chcę to zrobić w kodzie Java zamiast robić to w xml.

Jak ustawić paddingTop na <LinearLayout> w moim fragmencie kodu klasy Java ??

Author: AndyN, 2012-03-13

9 answers

view.setPadding(0,padding,0,0);

To ustawi Top padding na padding-pixels.

Jeśli chcesz ustawić go w dp zamiast tego, możesz wykonać konwersję:

float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (sizeInDp*scale + 0.5f);
 418
Author: Jave,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-11 07:02:23

Aby odpowiedzieć na drugie pytanie:

view.setPadding(0,padding,0,0);

Jak zasugerowano SpK i Jave, ustawi padding w pikselach. Możesz ustawić go w dp, obliczając wartość dp w następujący sposób:

int paddingDp = 25;
float density = context.getResources().getDisplayMetrics().density
int paddingPixel = (int)(paddingDp * density);
view.setPadding(0,paddingPixel,0,0);
Mam nadzieję, że to pomoże!
 81
Author: Chris Conway,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-10 05:46:25

Jeśli przechowujesz wypełnienie w plikach zasobów, możesz po prostu wywołać

    int padding = getResources().getDimensionPixelOffset(R.dimen.padding);

Robi nawrócenie za Ciebie.

 75
Author: Miklos Jakab,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-16 12:23:38

Możesz ustawić padding do widoku przez pro gramatycznie w poniższym kodzie -

view.setPadding(0,1,20,3);

A także dostępne są różne rodzaje wyściółek -

Padding

PaddingBottom

PaddingLeft

PaddingRight

PaddingTop

Te, linki będą odnosić Android Developers site. Mam nadzieję, że to wam pomoże.

 12
Author: Praveenkumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-13 14:15:10

Użycie TypedValue jest znacznie czystszym sposobem konwersji na piksele w porównaniu z ręcznym obliczaniem:

float paddingDp = 10f;
// Convert to pixels
int paddingPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, paddingDp, context.getResources().getDisplayMetrics());
view.setPadding(paddingPx, paddingPx, paddingPx, paddingPx);

Zasadniczo, TypedValue.applyDimension konwertuje żądane wypełnienie na piksele odpowiednio w zależności od właściwości wyświetlania bieżącego urządzenia.

Aby uzyskać więcej informacji zobacz: TypedValue.applyDimension Docs .

 7
Author: i2097i,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-09 18:48:14

Użyj poniższej metody do dynamicznego ustawiania wypełnienia

setPadding(int left, int top, int right, int bottom)

Przykład:

view.setPadding(2,2,2,2);
 5
Author: saigopi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-07 05:47:18

Napisz następujący kod, aby ustawić padding, może Ci to pomóc.

  TextView ApplyPaddingTextView = (TextView)findViewById(R.id.textView1);
  final LayoutParams layoutparams = (RelativeLayout.LayoutParams) ApplyPaddingTextView.getLayoutParams();

  layoutparams.setPadding(50,50,50,50);

  ApplyPaddingTextView.setLayoutParams(layoutparams);

Użyj LinearLayout.LayoutParams lub RelativeLayout.LayoutParams zgodnie z układem nadrzędnym widoku potomnego

 3
Author: Manikandan K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-13 13:46:57

Podczas wypełniania programowo, Konwertuj na wartości gęstości, konwertując piksel na Dp.

 1
Author: Bhagya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-17 13:27:10

Context contect=MainActivity.this; TextView tview=new TextView(context); tview.setPaddingRelative (10,0,0,0);

 0
Author: Siddhartha Halder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-15 20:30:11