does minHeight do anything?

2020-05-29 15:23发布

问题:

In the attached image, I want the column of buttons to match the height of the image, but I also want there to be a minimum height for the column of buttons.

It correctly matches the height of the image, but does not respect the minHeight, and will smoosh the buttons down.

I am setting these properties for the column of buttons:

<LinearLayout
   ...
   android:layout_alignTop="@+id/image"
   android:layout_alignBottom="@+id/image"
   android:minHeight="150dp"
   >

回答1:

I don't know all your exact requirements, but it seems you can solve this with another layer pretty much like in your diagram. Set the minHeight on an outer layout and then just fill_parent/match_parent on the inside. Maybe something like:

<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:minHeight="150dp">
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content">
    </LinearLayout>
    <ImageView />
</LinearLayout>


回答2:

Tricky question because it calls TextView.setMinHeight — but then you are not using a TextView.

So generally android:minHeight does indeed do something but not in your particular case.



回答3:

Although my other answer is still valid, nowadays we have the benefit of ConstraintLayout. I'm sure the original poster has long since gotten past this problem, but for future users' sake: you can achieve the same result without the extra layer of ViewGroups. Something like this:

<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="wrap_content"
    android:minHeight="150dp">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="Button1"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="Button2"
        app:layout_constraintBottom_toTopOf="@+id/button3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="Button3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="end"
        app:constraint_referenced_ids="button1,button2,button3" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/barrier"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Possibly you may need something like:

    app:layout_constrainedWidth="true"

for the ImageView to handle larger images, but I haven't tested it.