简体   繁体   English

如何在RelativeLayout的底部对齐LinearLayout?

[英]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. 我试图使LinearLayout在此RelativeLayout的底部对齐。 I tried setting alignParentBottom="true" , but then I can't even see the contents of the LinearLayout. 我尝试设置alignParentBottom="true" ,但是后来我什至看不到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" 添加到LinearLayoutandroid: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" . 尝试使用同时包含两个布局的LinearLayout并将它们设置为android:layout_weightandroid: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 . 不要忘记将权重之间的比例设置为所需的比例,并将容器的android:orientationvertical

BTW, for textSize you better use sp and not dp... 顺便说一句,对于textSize您最好使用sp而不是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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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