【发布时间】:2017-12-15 12:18:02
【问题描述】:
我的MainActivity布局是`LinearLayout,垂直方向,分为两半。上半部分的权重为 10,包含一个 ImageView。后半部分用于三个按钮。
按钮宽度应为 wrap_content,但至少为 230dp 宽 = 没问题。
现在我的问题:
我希望按钮的高度至少与正确显示按钮文本所需的高度一样高。但!如果还有更多空间,那么按钮应该至少 60dp 高。我怎样才能做到这一点?
我尝试将 layout_height 设置为:wrap_content 和 minHeight="60dp"。但是如果屏幕太小,那么屏幕上可能不会显示/出现一两个按钮。
所以我想到了 Height="match_parent" 并设置了一个 max_height="60dp" 。但这无济于事。无论如何,每个按钮都会覆盖 1/3 的区域。
这里有图片说明:https://imgur.com/a/TUkZd
我想要屏幕上 50% 的图片,下 50% 包含 3 个按钮。您至少应该能够阅读按钮 text = wrap_content... 但如果在较低的 50% 中有很多空间。那么按钮可以大一点。我无法做到这一点。
XML 代码:
<?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:background="@color/sligthly_grey"
android:orientation="vertical"
tools:context="com.example.MainActivity">
<ImageView
android:id="@+id/imageView_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="10"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:srcCompat="@drawable/img_picture"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="20dp">
<Button
android:minWidth="230dp"
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Btn one"
android:textSize="18sp" />
<Button
android:minWidth="230dp"
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Btn two"
android:textSize="18sp" />
<Button
android:minWidth="230dp"
android:id="@+id/btn_three"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Btn three"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
-
添加预期的布局图片
-
i.imgur.com/ljqPRUz.png 图片很大。所以它应该以某种方式安装在上半部分内,而不用拧紧它的尺寸。图片周围可能有空间...
-
这是什么?它只是一个白色图像,中间有文字。
-
这是一个示例图像,其宽度和高度与应用程序中使用的原始 im 完全相同:)
-
你想让它在每个屏幕尺寸上成正方形吗?