简体   繁体   中英

Two fragments in one Activity, proportions

I have two fragments. When I start the Activity only list_fragment is added to the container it takes 100% of the screen. Now I want to add the new_fragment to the same container. list_fragment should take 80% of the screen and new_fragment should take 20% of it (the rest).

list_fragment.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b6b6b6">


  <ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/listViewRecs"
    android:layout_gravity="left|center_vertical"
     />

</LinearLayout>

new_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />
 </LinearLayout>

container:

<LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
  >

</LinearLayout>

For propotions in LinearLayout android suggests using Layout Weights .

To achieve it you should set list_fragment layout_weight to 8, and new_fragment layout_weight to 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM