【问题标题】:How to align button to bottom of screen in Nested Linear Layouts [duplicate]如何在嵌套线性布局中将按钮与屏幕底部对齐[重复]
【发布时间】:2018-06-18 01:57:54
【问题描述】:

我正在尝试将按钮与屏幕底部对齐,但它没有移动。 最后一个按钮,id=button 1,是有问题的按钮。我希望这个按钮在屏幕底部右对齐,但看起来,嵌套数组的某些属性组合不会让它发生。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.crackit.crackit.startQuiz1">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"/>
<LinearLayout
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:weightSum="2">
<Button
android:id="@+id/radio0"
android:layout_weight="1"
android:layout_height="140dp"
android:layout_width="140dp"
/>

<Button
android:id="@+id/radio1"
android:layout_weight="1"
android:layout_height="140dp"
android:layout_width="140dp"
/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/radio2"
android:layout_weight="1"
android:layout_height="140dp"
android:layout_width="140dp"
/>

<Button
android:id="@+id/radio3"
android:text="Android"
android:layout_weight="1"
android:layout_height="140dp"
android:layout_width="140dp"
/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#0000FF"
android:onClick="NextQuestion"
android:layout_gravity="bottom"
android:textColor="#FFFFFF"
android:text="Next" />
</LinearLayout>
</LinearLayout>

我正在尝试将按钮与屏幕底部对齐,但它没有移动。 最后一个按钮,id=button 1,是有问题的按钮。我希望这个按钮在屏幕底部右对齐,但看起来,嵌套数组的某些属性组合不会让它发生。

【问题讨论】:

    标签: android


    【解决方案1】:

    使用此代码解决您的问题

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="40dp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="50dp"
            android:orientation="horizontal"
            android:weightSum="2">
    
            <Button
                android:id="@+id/radio0"
                android:layout_width="140dp"
                android:layout_height="140dp"
                android:layout_weight="1" />
    
            <Button
                android:id="@+id/radio1"
                android:layout_width="140dp"
                android:layout_height="140dp"
                android:layout_weight="1" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2">
    
            <Button
                android:id="@+id/radio2"
                android:layout_width="140dp"
                android:layout_height="140dp"
                android:layout_weight="1" />
    
            <Button
                android:id="@+id/radio3"
                android:layout_width="140dp"
                android:layout_height="140dp"
                android:layout_weight="1"
                android:text="Android" />
        </LinearLayout>
    
    </LinearLayout>
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="bottom"
        android:background="#0000FF"
        android:onClick="NextQuestion"
        android:text="Next"
        android:textColor="#FFFFFF" />
        </RelativeLayout>
    

    【讨论】:

    • 感谢 Sourabh Vadhva,它就像一个魅力......在 SO 上有答案,但我不想使用相对布局
    • 那你要使用哪种布局?请建议我去做
    • 现在我将 RelativeLayout 替换为 LinearLayout。请再次检查我的代码并告诉我。谢谢
    【解决方案2】:

    您可以使用RelativeLayout,然后将Button 相对于它对齐。以下是您的案例的示例。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    tools:context="com.crackit.crackit.startQuiz1">
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="40dp"/>
        <LinearLayout
            android:layout_marginTop="50dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal"
            android:weightSum="2">
            <Button
                android:id="@+id/radio0"
                android:layout_weight="1"
                android:layout_height="140dp"
                android:layout_width="140dp"
                />
    
            <Button
                android:id="@+id/radio1"
                android:layout_weight="1"
                android:layout_height="140dp"
                android:layout_width="140dp"
                />
    
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2">
            <Button
                android:id="@+id/radio2"
                android:layout_weight="1"
                android:layout_height="140dp"
                android:layout_width="140dp"
                />
    
            <Button
                android:id="@+id/radio3"
                android:text="Android"
                android:layout_weight="1"
                android:layout_height="140dp"
                android:layout_width="140dp"
                />
        </LinearLayout>
    
    </LinearLayout>
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#0000FF"
        android:onClick="NextQuestion"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:textColor="#FFFFFF"
        android:text="Next" />
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      如果想让按钮右对齐,把它的width属性改成

      android:layout_width="wrap_content"
      

      然后在你的第二个线性布局中添加一个属性“重力”:像这样 -

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="right" 
      android:layout_gravity="center"
      android:orientation="vertical" >
      

      如果您必须使用线性布局,请在按钮之前包含另一个空的线性布局,权重为 1 以用完剩余空间。 (所以那个按钮会在底部)像这样-

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-05
        • 1970-01-01
        相关资源
        最近更新 更多