android - 如何使按钮的高度根据屏幕自动变化?
问题描述
我有 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>
在这里,我给你留下一张关于我希望它看起来如何的编辑图像:
谢谢你的帮助。(我认为在 layout_height 中使用 match_parent 将是解决方案,但我已经尝试过但我不知道如何使用它)
解决方案
如果要使用 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>
推荐阅读
- android - 在所有活动中共享相同的片段状态
- google-cloud-firestore - Kivy buildozer Firestore 错误 - ModuleNotFoundError:没有名为“google.cloud”的模块
- html - 如何在vuetify中居中对齐一组浮动按钮?
- css - 如何使用 vuetify 制作移动响应式导航栏
- swift - 致命错误:收到我标记为可选的 nil 数据时索引超出范围
- haskell - 在 Haskell 中结合多种过滤功能的优雅方式
- php - 了解 PHP 中的素数分解函数?
- swift - Swift - 方法返回后从firebase实时数据库获取数据
- video - FFmpeg 不正确地渲染面向纵向的 .mov 文件
- python-3.x - 如何使用 python 在 Telegram bot 中发送表情符号?