簡體   English   中英

如何在RelativeLayout的底部對齊LinearLayout?

[英]How to align LinearLayout at bottom in a RelativeLayout?

我試圖使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>

添加到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">
 [...]
<LinearLayout 
 android:layout_alignParentBottom="true" 
 android:layout_height="100dp" 
 android:layout_width="wrap_content">
</LineaLayout>

嘗試使用同時包含兩個布局的LinearLayout並將它們設置為android:layout_weightandroid:layout_height="0dp"
不要忘記將權重之間的比例設置為所需的比例,並將容器的android:orientationvertical

順便說一句,對於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