Añadir relleno en la vista mediante programación
Estoy deveoping Android v2. 2 aplicación.
Tengo un fragmento.
En la devolución de llamada onCreateView(...)
de mi clase fragment, inflo un diseño para el fragmento como se muestra a continuación:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login, null);
return view;
}
El archivo de diseño inflado anterior es (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>
Me gustaría establecer un paddingTop
para el elemento anterior <LinearLayout>
, y quiero hacerlo en el código Java en lugar de hacerlo en xml.
¿Cómo establecer paddingTop
a <LinearLayout>
en mi código de clase Java fragment ??
9 answers
view.setPadding(0,padding,0,0);
Esto establecerá el relleno superior en padding
-píxeles.
Si desea configurarlo en dp
en su lugar, puede hacer una conversión :
float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (sizeInDp*scale + 0.5f);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-11 07:02:23
Para responder a su segunda pregunta:
view.setPadding(0,padding,0,0);
Como Spk y Jave sugirieron, establecerá el relleno en píxeles. Puede configurarlo en dp calculando el valor dp de la siguiente manera:
int paddingDp = 25;
float density = context.getResources().getDisplayMetrics().density
int paddingPixel = (int)(paddingDp * density);
view.setPadding(0,paddingPixel,0,0);
¡Espero que eso ayude!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-10 05:46:25
Si almacena el relleno en archivos fuente, simplemente puede llamar a
int padding = getResources().getDimensionPixelOffset(R.dimen.padding);
Hace la conversión por ti.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-16 12:23:38
Puede configurar el relleno para su vista por pro gramatically a lo largo del siguiente código -
view.setPadding(0,1,20,3);
Y, también hay diferentes tipos de relleno disponibles -
Estos, enlaces se refieren a los desarrolladores de Android sitio. Espero que esto les ayude mucho.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-13 14:15:10
Usar TypedValue
es una forma mucho más limpia de convertir a píxeles en comparación con el cálculo manual:
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);
Esencialmente, TypedValue.applyDimension
convierte el relleno deseado en píxeles apropiadamente dependiendo de las propiedades de visualización del dispositivo actual.
Para más información ver: TypedValue.applyDimension Docs .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-09 18:48:14
Utilice el siguiente método para configurar el relleno dinámicamente
setPadding(int left, int top, int right, int bottom)
Ejemplo:
view.setPadding(2,2,2,2);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-07 05:47:18
Escriba el Siguiente código para configurar el relleno, puede ayudarlo.
TextView ApplyPaddingTextView = (TextView)findViewById(R.id.textView1);
final LayoutParams layoutparams = (RelativeLayout.LayoutParams) ApplyPaddingTextView.getLayoutParams();
layoutparams.setPadding(50,50,50,50);
ApplyPaddingTextView.setLayoutParams(layoutparams);
Use LinearLayout.LayoutParams o RelativeLayout.LayoutParams según la disposición principal de la vista secundaria
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-13 13:46:57
Mientras que el relleno programáticamente, convertir a valores relacionados con la densidad mediante la conversión de píxeles a Dp.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-17 13:27:10
Context contect=MainActivity.this; TextView tview = new TextView (context);tview.setPaddingRelative (10,0,0,0);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-15 20:30:11