fragmento de cambio de tamaño dinámico android
Frecuentes
Visto 24,588 veces
5
¿Quiero cambiar el ancho de un fragmento que estoy usando en mi MaiActivity? Pero no puedo encontrar su LayoutParams
para cambiar su ancho predeterminado:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int widthFfragment = 300;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
widthTot = size.x;
heightTot = size.y;
findViewById(R.id.f1).getLayoutParams().width = widthTot-widthFfragment;
}
}
Este es el código de mi fragmento:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/f1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#330000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Fragmento1.java
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//---Inflate the layout for this fragment---
return inflater.inflate( R.layout.fragment1, container, false);
}
}
Muchas gracias.
2 Respuestas
12
Puede hacerlo trabajando con la instancia de LayoutParams de la Vista obtenida de Fragment.getView(). Mi código de muestra:
private void resizeFragment(Fragment f, int newWidth, int newHeight) {
if (f != null) {
View view = f.getView();
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(newWidth, newHeight);
view.setLayoutParams(p);
view.requestLayout();
}
}
Y se puede usar de la siguiente manera:
resizeFragment(myFragment, 200, 500);
resizeFragment(otherFragment, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
contestado el 07 de mayo de 14 a las 16:05
4
Tiene razón, el Fragmento no tiene LayoutParams porque es un contenedor conceptual, debe ajustar los parámetros de diseño de la vista inflada en el fragmento o al contenedor al que adjunta el fragmento.
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate( R.layout.fragment1, container, false);
// TODO Adjust layout params of inflated view here
return view;
}
El ejemplo asume que está inflando un ViewGroup.
Espero que ayude.
contestado el 22 de mayo de 12 a las 11:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android android-fragments or haz tu propia pregunta.
Gracias, necesito ajustarme en la actividad principal, ¿es posible? - bill23
Lo resuelvo con LayoutParams y establezco el tamaño apropiado. muchas gracias - bill23