【问题标题】:does minHeight do anything?minHeight 有什么作用吗?
【发布时间】:2018-02-19 14:44:34
【问题描述】:

在附加的图像中,我希望按钮列与图像的高度相匹配,但我也希望按钮列有一个最小高度。

它正确匹配图像的高度,但不遵守 minHeight,并将按钮向下平滑。

我正在为按钮列设置这些属性:

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

【问题讨论】:

    标签: android layout


    【解决方案1】:

    我不知道您的所有确切要求,但您似乎可以使用另一个层来解决这个问题,就像您的图表中一样。在外部布局上设置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>
    

    【讨论】:

    • 但请记住:如果您使用多种布局,您的应用程序在启动时可能会变得迟缓。似乎布局在启动时需要大量内存,这可能会触发垃圾收集,从而减慢您的应用程序,直到再次释放启动所需的内存。是的,我有这个问题,目前我尝试改用 RelativLayout。
    【解决方案2】:

    棘手的问题,因为它调用了TextView.setMinHeight——但是你没有使用TextView

    所以通常android:minHeight 确实会做一些事情,但在您的特定情况下不会。

    【讨论】:

    • 这是对原始问题的一个很好的回答。它可能无法帮助用户实现他们的最终目标,但我发现它仍然很有趣。
    • 这是完全错误的。通常,视图的 minHeight 是一个有效属性。它的程序等价物是developer.android.com/reference/android/view/…
    • @David Liu 该属性在 XML 中有效并不意味着它存储在视图中。即使属性存储在视图中,也不意味着它实际上被活动布局管理器使用。为了让它更有趣:有一个TextView.setMinHeight 和一个View.setMinimumHeight
    • 重点是提出 TextView.setMinHeight 完全无关紧要,因为问题不涉及文本视图。至于 XML 的有效性,它被清楚地使用并存储在基 View 类中,如下所示:android.googlesource.com/platform/frameworks/base/+/refs/heads/… 第 970 行。活动布局管理器是否实际使用它与我的观点无关(尽管如所见在接受的答案中,该 minHeight 属性与实际答案相关,因为 LinearLayout 确实考虑了 View.minHeight)。
    【解决方案3】:

    虽然我的其他答案仍然有效,但现在我们有ConstraintLayout 的好处。我敢肯定原来的发帖人早就解决了这个问题,但是为了未来的用户:你可以在没有ViewGroups 的额外层的情况下达到相同的结果。像这样的:

    <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 可以处理较大的图像,但我没有测试过。

    【讨论】:

      猜你喜欢
      • 2017-10-10
      • 2016-01-13
      • 2011-10-09
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      相关资源
      最近更新 更多