Android - 设置了layout_weight的多行按钮的高度

标签 android android-layout android-button

我的对话框有以下布局:

<?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"
    android:orientation="vertical">

    <TextView
        style="@style/TextAppearance.AppCompat.Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="@string/practice_question" />

    <TextView
        android:id="@+id/dialog_select_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:textColor="@color/primary_text"
        android:textSize="16sp"
        tools:ignore="RtlHardcoded" />

    <ListView
        android:id="@+id/dialog_select_answers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/dialog_select_board_button"
            style="?attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:text="@string/practice_look_board" />

        <Button
            android:id="@+id/dialog_select_answer_button"
            style="?attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:text="@string/practice_answer" />
    </LinearLayout>

</LinearLayout>

底部按钮有问题: Problem

我希望第一个按钮不被裁剪。我已经设置了 android:layout_height="wrap_content" 但这没有帮助。

附注如果我将按钮文本从“在板上查看”更改为“在板上查看”,一切正常。但我想让第二个按钮可选,因此第一个按钮可能与整个父级的宽度匹配。

最佳答案

我更改了您的一些代码,请尝试一下

<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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialog_select_title"
        style="@style/TextAppearance.AppCompat.Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="@string/practice_question" />

    <TextView
        android:id="@+id/dialog_select_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dialog_select_title"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:textColor="@color/primary_text"
        android:textSize="16sp"
        tools:ignore="RtlHardcoded" />

    <ListView
        android:id="@+id/dialog_select_answers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dialog_select_question" />

    <LinearLayout
        android:id="@+id/dialog_select_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dialog_select_answers"
        android:orientation="horizontal">

        <Button
            android:id="@+id/dialog_select_board_button"
            style="?attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:text="@string/practice_look_board" />

        <Button
            android:id="@+id/dialog_select_answer_button"
            style="?attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:text="@string/practice_answer" />
    </LinearLayout>

</RelativeLayout>

关于Android - 设置了layout_weight的多行按钮的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40927107/

相关文章:

java - 从android中listAdapter之外的viewList访问 View 项

java - 在不配对的情况下使用带 BLE 的 Rfcomm 套接字?

android - AppBarLayout 与 RecyclerView 重叠

java - 仅为导航 View 中的一个 fragment 折叠工具栏

android-layout - 在 Android 中以编程方式实现切换按钮的 "android:textOff"和 "android:textOn"属性

android - 4 次电源按钮按下通话功能

android图像旋转,导入错误

android - 如何在不将 App Theme 更改为 Custom Theme 的情况下制作自定义标题栏?

android - 如何使用单元测试来测试按钮是否启动了正确的 Activity

Android 3D按钮文字位置