2011-09-13 156 views
22

在附图中,我希望按钮的列与图像的高度相匹配,但我也希望按钮列的高度最小。做minHeight做什么?

它正确匹配图像的高度,但不尊重minHeight,并会按下按钮。

我为按钮栏设置这些属性:

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

enter image description here

回答

38

我不知道你所有的具体要求,但似乎你可以与其他层解决这个很像在你的图中。在外部布局上设置minHeight,然后在内部设置fill_parent/match_parent。也许是这样的:

<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

但请记住:如果您使用多种布局,您的应用程序在启动时可能会变得缓慢。似乎布局在启动时需要大量内存,这可能会触发垃圾收集,从而减慢应用程序的运行速度,直到启动所需的内存再次释放为止。是的,我有这个问题,目前我尝试切换到RelativLayout。 – Martin

6

棘手的问题,因为它要求TextView.setMinHeight - 但你不使用TextView

因此,一般android:minHeight确实做了一些事情,但不是在你的具体情况。

+5

这是对原问题的一个很好的回答。它可能不会帮助用户达到最终目标,但我发现它很有趣。 – jwir3

+1

这完全不正确。一般来说minHeight对于视图来说是一个有效的属性。它的编程等效是https://developer.android.com/reference/android/view/View.html#setMinimumHeight(int) –

+1

@David Liu该属性在XML中有效并不意味着它存储在视图。 即使该属性存储在视图中,也不表示该值实际上是由活动布局管理器使用的。 为了让它更有趣:有一个'TextView.setMinHeight'和一个'View.setMinimumHeight' – Martin

0

虽然我的其他答案仍然有效,但现在我们有ConstraintLayout的好处。我确定原来的海报早已过去了这个问题,但为了未来用户的缘故:如果没有额外的ViewGroup秒,您可以达到相同的结果。事情是这样的:

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

也许你可能需要类似:

app:layout_constrainedWidth="true" 

ImageView处理更大的图像,但我没有测试它。