簡體   English   中英

LinearLayout按鈕的高度應為match_parent但為maxHeight,這可能嗎?

[英]LinearLayout Button height should match_parent but with maxHeight, is this possible?

我的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>

您需要使用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>

我從布局后的問題中得到的答案應根據需要工作。

<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則將其與left和right padding屬性一起使用。 下面是上面的輸出。您需要為橫向模式創建其他布局。 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM