【问题标题】:LinearLayout Button height should match_parent but with maxHeight, is this possible?LinearLayout Button 的高度应该 match_parent 但使用 maxHeight,这可能吗?
【发布时间】: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 完全相同:)
  • 你想让它在每个屏幕尺寸上成正方形吗?

标签: java android xml layout


【解决方案1】:

您需要使用scrollview,这样如果屏幕很小,其他buttons 应该会显示并提供minHeightminWidth 属性。使用Scrollview 将解决您的问题。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

<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="wrap_content"
    android:background="@color/grey"
    android:orientation="vertical">


    <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/ic_delete_grey"
        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:id="@+id/btn_one"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:minHeight="60dp"
            android:minWidth="230dp"
            android:text="Btn one"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_two"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:minHeight="60dp"
            android:minWidth="230dp"
            android:text="Btn two"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_three"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:minHeight="60dp"
            android:minWidth="230dp"
            android:text="Btn three"
            android:textSize="18sp" />

    </LinearLayout>

</LinearLayout>
</ScrollView>

【讨论】:

    【解决方案2】:

    我从以下布局的问题中得到的应该根据需要工作。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/absoluteLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    
        <ImageView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:src="@drawable/rounded_square" />
    
    </RelativeLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Button" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="10dp"
            android:text="Button2" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_alignParentBottom="true"
            android:text="Button3" />
    
    </LinearLayout>
    

    如果您希望按钮宽度为wrap_content,请使用左右padding 属性。下面是上面的输出。您需要为横向模式创建其他布局。

    【讨论】:

    • 在这个例子中。可以清楚地看到按钮可能会更高一些。所以如果有空间,那么 minheight="60dp" 会很好。但是在横向模式下,当可能没有更多空间时,按钮的高度应该至少达到正确显示按钮文本所需的高度。
    • 对于横向,如果您使用带有 fitXY 的图像,则需要在横向中创建单独的布局,然后它会拉伸,否则不会在图像的两侧留下大量空间。关于按钮高度,您可以使用来自 dimen.xml 的不同 dp 尺寸作为所有屏幕尺寸的高度。
    猜你喜欢
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2011-05-19
    相关资源
    最近更新 更多