简体   繁体   中英

How to align LinearLayout at bottom in a RelativeLayout?

I'm trying to get the LinearLayout to be aligned at the bottom of this RelativeLayout. I tried setting alignParentBottom="true" , but then I can't even see the contents of the LinearLayout. Is there another way to accomplish what I need?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="90dp"
                android:layout_height="200dp"
                android:minHeight="200dp"
                android:background="@color/colorPrimaryDark"
                android:elevation="2dp"
    android:gravity="center">
    <TextView
        android:id="@+id/textView_bookName"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:padding="10dp"
        android:textSize="20dp"
        android:textColor="@color/white"
        />
    <TextView
        android:id="@+id/textView_bookAuthor"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="1dp"
        android:text="Author"
        android:textSize="10dp"
        android:textColor="@color/white"
        android:layout_below="@+id/textView_bookName"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@+id/textView_bookAuthor"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button_download_book"
            android:text="@string/download"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button_read_book"
            android:text="Read"
            android:visibility="gone"/>
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/progressbar_book_download"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:progress="0"
            android:visibility="gone"
            />
    </LinearLayout>
</RelativeLayout>

Add to LinearLayout : android:layout_alignParentBottom="true"

eg.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/textView_bookAuthor">
 [...]
<LinearLayout 
 android:layout_alignParentBottom="true" 
 android:layout_height="100dp" 
 android:layout_width="wrap_content">
</LineaLayout>

Try using a LinearLayout that contains both layouts and setting them an android:layout_weight and android:layout_height="0dp" .
Don't forget to set the ratio between the weights to one you'd like and setting the container's android:orientation to vertical .

BTW, for textSize you better use sp and not dp...

Please try below xml which I have modified. It will solve the issue. I have removed the  android:layout_below="@+id/textView_bookAuthor" and added  android:layout_alignParentBottom="true".

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="90dp"
    android:layout_height="200dp"
    android:minHeight="200dp"
    android:background="@color/colorPrimaryDark"
    android:elevation="2dp"
    android:gravity="center">

    <TextView
        android:id="@+id/textView_bookName"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:padding="10dp"
        android:textSize="20dp"
        />
    <TextView
        android:id="@+id/textView_bookAuthor"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="1dp"
        android:text="Author"
        android:textSize="10dp"
        android:layout_below="@+id/textView_bookName"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"        
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button_download_book"
           />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button_read_book"
            android:text="Read"
            android:visibility="gone"/>
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/progressbar_book_download"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:progress="0"
            android:visibility="gone"
            />
    </LinearLayout>
</RelativeLayout>

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