Domanda

I am using fragments in my Android layout. How to set the height of the fragments layout?

This is activity_main.xml

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fragment No.1"
    android:onClick="selectFrag" />

 <Button
     android:id="@+id/button2"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:onClick="selectFrag"
     android:text="Fragment No.2" /> 

  <fragment
    android:name="com.android.fragmentstest.FragmentOne"
    android:id="@+id/fragment_place"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />

 </LinearLayout>

This is fragment.xml

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="40dp"
    android:layout_height="20dp"
    android:id="@+id/tutlist_fragment"
    android:layout_weight="45"
    android:background="#7bae16">


   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:layout_weight="1"
       android:text="This is fragment No.1"
       android:textStyle="bold" />

       </RelativeLayout>

I need to set the maximum height of the fragment and the position of the fragment. How will I achieve that?

È stato utile?

Soluzione

I dont see what the big problem is. It is possible to set attributes of a Fragment just like any other view, (eg:height, weight ,etc)

So, the correct way to set the height of the fragment will be

This will be the activity_main.xml file You dont need to define Layout height in the individual fragments itself.

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No.1"
        android:onClick="selectFrag" />

     <Button
         android:id="@+id/button2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:onClick="selectFrag"
         android:text="Fragment No.2" /> 

   <fragment
       android:id="@+id/fragment_place"
       android:name="com.android.fragmentstest.FragmentOne"
       android:layout_width="match_parent"
       android:layout_height="90dp" />

</LinearLayout>

Note the Android:layout_height="90dp" defines the height of the fragment in the main activity.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top