android - 如何确保按钮在此 Android 登录对话框中水平对齐?

标签 android android-layout

我正在 Android 上创建一个登录对话框,我希望两个按钮在顶部和底部边缘水平对齐,而不管屏幕尺寸如何。有时“忘记密码”会分两行,有时则不会。我不想在按钮上强加 minHeight,并且更喜欢仅基于 XML 设计(而非代码)的解决方案。

风景很好:

Horizontal - ok

纵向,默认实现使登录按钮比“忘记密码”短

Vertical - break

我尝试的一个解决方案是为第一个按钮的 layout_height 参数设置 match_parent。不太奏效:

enter image description here

XML:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:id="@+id/terms_layout"
    android:layout_marginTop="10dp"
    >

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="@string/login"
        android:textSize="18sp"
        android:id="@+id/dialog_login_button_login"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/forgotPassword"
        android:textSize="18sp"
        android:id="@+id/dialog_login_button_forgot_password"
        />

</LinearLayout>

最佳答案

使用 RelativeLayout 并使“登录”按钮与“忘记密码”按钮的顶部和底部对齐

关于android - 如何确保按钮在此 Android 登录对话框中水平对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31539331/

相关文章:

android - 通过 ListView 编辑文本搜索中的挂起问题

java - Android 什么时候可以释放端口?

java - Android android.intent.action.SEND youtube 视频

android - 以编程方式组合几种类型的xml布局android

android - Imageview 不应用 fitSystemWindows

java - 想要在 Imagebutton 单击时创建 NavigationDrawer

android - RelativeLayout 添加规则 "RelativeLayout.LEFT_OF"不起作用

android - 为什么 wrap_content 是填充高度?

java - 具有任意深度嵌套项目的 Recyclerview

android - 无法使用 setLayoutParams 更改 View 的宽度/高度