首页 > 解决方案 > 如何使按钮的高度根据屏幕自动变化?

问题描述

我有 3 个位于图像视图下方的按钮,但我希​​望它们根据屏幕比例/大小自动更改高度。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
    tools:context=".Main3Activity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="144dp"
        android:layout_height="133dp"
        app:srcCompat="@android:drawable/btn_star_big_on"
        tools:layout_editor_absoluteX="133dp"
        tools:layout_editor_absoluteY="40dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/imageView"
        android:text="Button1"
        tools:layout_editor_absoluteX="36dp"
        tools:layout_editor_absoluteY="184dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/button1"
        android:text="Button2"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="260dp" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/button2"
        android:text="Button3"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="340dp" />



</android.support.constraint.ConstraintLayout>

这就是它在 6 英寸屏幕和 5 英寸屏幕中的样子: 在此处输入图像描述

在此处输入图像描述

在这里,我给你留下一张关于我希望它看起来如何的编辑图像:

在此处输入图像描述

在此处输入图像描述

谢谢你的帮助。(我认为在 layout_height 中使用 match_parent 将是解决方案,但我已经尝试过但我不知道如何使用它)

标签: androidandroid-layoutandroid-buttonandroid-relativelayout

解决方案


如果要使用 aLinearLayout并为所有 3 个按钮设置相同的权重:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:orientation="vertical"
    tools:context=".Main3Activity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="144dp"
        android:layout_height="133dp"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@android:drawable/btn_star_big_on" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:text="Button1"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:text="Button2"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:text="Button3"
        android:layout_weight="1" />
</LinearLayout>

推荐阅读