如何在RelativeLayout中将LinearLayout对齐在底部?

时间:2016-03-13 15:37:54

标签: android xml android-layout android-fragments

我正在尝试让LinearLayout在这个RelativeLayout的底部对齐。我尝试设置alignParentBottom="true",但后来我甚至看不到LinearLayout的内容。还有另一种方法可以实现我的需要吗?

<?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>

4 个答案:

答案 0 :(得分:1)

添加到LinearLayoutandroid:layout_alignParentBottom="true"

例如

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

答案 1 :(得分:0)

<LinearLayout 
 android:layout_alignParentBottom="true" 
 android:layout_height="100dp" 
 android:layout_width="wrap_content">
</LineaLayout>

答案 2 :(得分:0)

尝试使用包含两种布局的LinearLayout并将其设置为android:layout_weightandroid:layout_height="0dp"
不要忘记将权重之间的比率设置为您想要的权重,并将容器的android:orientation设置为vertical

BTW,对于textSize,你最好使用sp而不是dp .​​..

答案 3 :(得分:0)

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>