tutorial studio fragments example ejemplo activity android center android-linearlayout fragment framelayout

fragments - fragment example in android studio



Fragmento de Android no respeta match_parent como altura (4)

La forma en que lo hice fue crear una imagen transparente en código xml, hacer que el fragmento tenga un diseño relativo y hacer que layout_alignParentBottom="true" en ImageView , de esta manera la imagen se pegue a la parte inferior y haga que el fragmento llene el elemento primario

Código aquí:

temp_transparent.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:thickness="0dp" android:shape="rectangle"> <gradient android:startColor="#00000000" android:endColor="#00000000" android:type="linear" android:angle="270"/>

fragment_my_invites.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/my_invites_fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <ListView android:id="@+id/list_invites" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@color/list_divider" android:dividerHeight="1dp" android:layout_alignParentTop="true" > </ListView> <ImageView android:id="@+id/transparent_image" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_below="@id/list_invites" android:src="@drawable/temp_transparent" />

Lo siento por el enorme volcado de código, pero estoy realmente perdido.

MyActivity.java onCreate:

super.onCreate(savedInstanceState); setContentView(R.layout.activity_singlepane_empty); mFragment = new PlacesFragment(); getSupportFragmentManager().beginTransaction() .add(R.id.root_container, mFragment) .commit();

PlacesFragment.java enCreateView:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null); return mRootView;

Notas : mRootView es un ViewGroup global, no hay problema al respecto, creo. PlacesFragment es un ListFragment.

Diseños:

activity_singlepane_empty.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root_container" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00f"> <include layout="@layout/actionbar"/> <!-- FRAGMENTS COME HERE! See match_parent above --> </LinearLayout>

list_content.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listContainer" android:background="#990" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false" /> <TextView android:id="@id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/no_data_suggest_connection"/> </FrameLayout>

Problema: como puede ver, el comportamiento esperado sería tener el TextView vacío arriba para que aparezca centrado en la pantalla. En la vista previa de diseño en Eclipse, está bien. Solo cuando se agrega a root_view como un fragmento, FrameLayout no llenará toda la pantalla.

root_container es azul, y FrameLayout es amarillento, ver más abajo para propósitos de depuración. ¿No debería el panel amarillo llenar toda la pantalla?!?!?!?


Por alguna razón, FrameLayout no obtiene su diseño del XML.

Necesito configurarlo también en el código (Fragment''s onCreateView):

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null); FrameLayout fl = (FrameLayout) mRootView.findViewById(R.id.listContainer); fl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); return mRootView;


Tuve el mismo problema y creo que sucede cuando inflas el diseño en el onCreateView del Fragmento con null, como hiciste aquí:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);

En su lugar tienes que hacer esto:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content,container, false);

Donde contenedor es el Viewgroup. Al menos, eso me solucionó el problema.


<android.support.v4.widget.NestedScrollView android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:fillViewport="true" > <include layout="@layout/screen_main_fragment" /> </android.support.v4.widget.NestedScrollView>

Tuve que cambiar layout_height = "wrap_content" por "match_parent" para que funcione.