【问题标题】:How to make the button's height change automatically depending on the screen?如何使按钮的高度根据屏幕自动变化?
【发布时间】:2019-05-03 13:52:12
【问题描述】:

我有 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 将是解决方案,但我尝试过但我不知道如何使用它)

【问题讨论】:

    标签: android android-layout android-button android-relativelayout


    【解决方案1】:

    如果您想使用 LinearLayout 并为所有 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>
    

    【讨论】:

      【解决方案2】:

      您可以使用 LinearLayout 或 TableLayout 并让所有按钮的高度 match_parent 然后给所有按钮 weight 属性

      android:layout_weight="1"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-24
        • 1970-01-01
        • 1970-01-01
        • 2012-06-17
        • 1970-01-01
        • 1970-01-01
        • 2019-05-19
        • 1970-01-01
        相关资源
        最近更新 更多