水平线性布局中的右对齐按钮

新手上路,请多包涵

如果您查看随附的图像。我需要我的按钮右对齐,但由于某种原因它不能与“重力:右”一起使用……

取消 添加

这是我的该布局的代码:

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

为什么没有工作?!

原文由 user818700 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 501
2 个回答

使用下面的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="9dp"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/stitch_button"
        android:text="@string/add" />

</RelativeLayout>

原文由 Dipak Keshariya 发布,翻译遵循 CC BY-SA 4.0 许可协议

这将在不添加任何其他视图的情况下工作。

您需要将您的 线性布局高度设置为 wrap-content 以便它占据所包含小部件 s 的最大尺寸。然后我们将使用 设置为 center_vertical 的重力 对齐它。

 <?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:paddingHorizontal="8dp"
    tools:context=".MainActivity">

    <Button
        android:paddingVertical="40"
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start"
        />

    <Button
        android:id="@+id/pauseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pause"
        />

    <Button
        android:id="@+id/resetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

原文由 UTKARSH Sharma 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题