简体   繁体   中英

How to make sure all parts of my layout stay visible?

I have a dialogfragment that has the following layout:

<?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:minWidth="550dp"
    android:orientation="vertical"
    android:padding="12dp">

    <TextView
        android:id="@+id/firstText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dp"
        android:paddingTop="2dp"
        android:text="@string/selection"
        android:textStyle="bold" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:background="@color/black_background_color">

    </android.support.v7.widget.RecyclerView>

    <TextView
        android:id="@+id/secondText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dp"
        android:paddingTop="2dp"
        android:text="@string/chosenTabs"
        android:textStyle="bold" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/usedTabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:background="@color/black_background_color" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="end"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:padding="4dp">

        <TextView
            android:id="@+id/cancelText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:text="@string/cancel"
            android:textColor="@color/black_settings_accent_color"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/confirmText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/accept"
            android:textColor="@color/black_settings_accent_color"
            android:textStyle="bold" />

    </LinearLayout>
</LinearLayout>

As you see, it contains two textviews, two recyclerviews and two buttons.

One recyclerview is filled with a certain amount of items (8), the other one can contain any number of items. I thought this wouldn't be a problem. But if I add too many elements, the second view expands and pushes my buttons from visibility. How can I ensure the buttons have priority and always have enough room?

Try this:

<?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:minWidth="550dp"
              android:orientation="vertical"
              android:padding="12dp">

    <TextView
        android:id="@+id/firstText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dp"
        android:paddingTop="2dp"
        android:text="@string/selection"
        android:textStyle="bold"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/black_background_color"/>

    <TextView
        android:id="@+id/secondText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dp"
        android:paddingTop="2dp"
        android:text="@string/chosenTabs"
        android:textStyle="bold"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/usedTabs"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/black_background_color"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:orientation="horizontal"
        android:padding="4dp">

        <TextView
            android:id="@+id/cancelText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:text="@string/cancel"
            android:textColor="@color/black_settings_accent_color"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/confirmText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/accept"
            android:textColor="@color/black_settings_accent_color"
            android:textStyle="bold"/>

    </LinearLayout>
</LinearLayout>

There is so many ways for that ..
1) set whole view in ScrollView.
2) set fixed sizes for RecyclerView so it will be inner scrolled.
3) export the last LinearLayout out of it's parent and set both in RelativeLayout, and set the buttons LinearLayout android:layout_alignParentBottom="true"

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