Añadir diseño con parámetros programáticamente Android
Hola, tengo una pantalla principal.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment"/> <ImageButton android:id="@+id/imageButton1" android:layout_width="55dp" android:layout_height="50dp" android:layout_alignParentTop="true" android:layout_marginTop="10dp" android:layout_toLeftOf="@+id/toggleButton1" android:background="@android:drawable/btn_default" android:contentDescription="@string/app_name" android:scaleType="centerCrop" android:src="@drawable/plus_grey" /> <LinearLayout android:id="@+id/lay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_marginEnd="0dp" android:background="#A0FFFFFF" android:orientation="horizontal" android:visibility="invisible" > <TextView android:id="@+id/locinfo" android:layout_width="match_parent" android:layout_height="60dp" android:textColor="@color/cherniy" android:textSize="20sp" android:visibility="visible" /> </LinearLayout> </RelativeLayout>
Y hay un imageButton
- Android Salir del modo de pantalla completa
- ¿Cómo configurar layout_alignParentRight = "false" mediante programación?
- Los métodos getTop (), getLeft (), getX (), getY (), getWidth (), getHeight () de la vista de Android
- cómo establecer el relleno o el margen a la disposición lineal?
- Restablecer LayoutParams a lo que se define en XML
button = (ImageButton) findViewById(R.id.imageButton1); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (bIcon == false) { button.setImageResource(R.drawable.plus_yellow); m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true)); LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay); t = (TextView)findViewById(R.id.locinfo); linLayout.setVisibility(View.VISIBLE); t.setVisibility(View.VISIBLE); bIcon = true; } else { button.setImageResource(R.drawable.plus_grey); m.remove(); LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay); t.setVisibility(View.INVISIBLE); linLayout.setVisibility(View.INVISIBLE); bIcon = false; } } });
Quiero agregar mediante programación
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);
Donde w
display = getWindowManager().getDefaultDisplay(); @SuppressWarnings("deprecation") w = display.getWidth();
Pero cuando me gusta esto
button.setImageResource(R.drawable.plus_yellow); m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true)); LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT); linLayout.setLayoutParams(lp); t = (TextView)findViewById(R.id.locinfo); linLayout.setVisibility(View.VISIBLE); t.setVisibility(View.VISIBLE); bIcon = true;
Mi aplicación se bloquea. ¿Puede por favor decir cómo crear mediante programación con mis parámetros (atado con el ancho y la altura de la pantalla del dispositivo)? Tomaré cualquier sugerencia. Gracias.
- Cómo usar el arrastrar correctamente de una vista en android
- ¿Qué unidad de medida utiliza LayoutParams?
- Android: cambiar LayoutParams de vista agregado por WindowManager
- ¿Cómo ampliar la vista sin problemas en Android?
- Creación de una vista redimensionable ListAdapter-recycleable
- Configuración de los parámetros de diseño en android
- vista de búsqueda no expandir todo el ancho
- Configuración del margen del botón mediante programación
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( (w*2)/3, LayoutParams.WRAP_CONTENT ); linLayout.setLayoutParams(lp);
Esto arrojará error porque el LinearLayout
es el hijo de la disposición relativa, así que usted tiene que fijar RelativeLayoutParams
para eso.
Utilice este
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( (w*2)/3, RelativeLayout.LayoutParams.WRAP_CONTENT ); linLayout.setLayoutParams(lp);
O En lugar de crear nuevos LayoutParams
, puede reutilizar los LayoutParams
existentes de la vista, como se muestra a continuación.
RelativeLayout.LayoutParams lp = linLayout.getLayoutParams(); lp.width = (w*2)/3; lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT; linLayout.setLayoutParams(lp);
En lugar de hacer un nuevo LayoutParams, debe modificar el existente.
Cambie su código a:
button.setImageResource(R.drawable.plus_yellow); m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true)); LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay); LinearLayout.LayoutParams lp = linLayout.getLayoutParams(); final int left_margin = whatever; final int top_margin = 0; final int right_margin = whatevermore; final int bottom_margin = 0; lp.setMargins(left_margin, top_margin, right_margin, bottom_margin); lp.width = (w*2)/3; linLayout.setLayoutParams(lp); t = (TextView)findViewById(R.id.locinfo); linLayout.setVisibility(View.VISIBLE); t.setVisibility(View.VISIBLE); bIcon = true;
- ¿Cómo eliminar el permiso específico al crear una aplicación para Android con gradle?
- Fragmento: qué devolución de llamada se invocó al pulsar el botón de retroceso y personalizarlo