【问题标题】:Children views with equal height based view with greatest height具有相同高度的子视图,具有最大高度的视图
【发布时间】:2018-06-02 02:35:43
【问题描述】:

我正在尝试将在 ios 上非常容易的东西复制到 android。

我想要在运行时确定高度的相同高度的视图。视图的高度应以高度最大的视图为准。

我发现了一个类似的问题 (link),但它是基于按钮的,这可能是我的解决方案不起作用的原因。

例子:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:baselineAligned="false">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    <!--Other views-->
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    <!--Other views-->
    </LinearLayout>

</LinearLayout>

也许有一个使用设计器的解决方案。 如果不可能,我认为我可能必须通过找到最大高度然后将高度分配给其他视图来以编程方式进行操作(如果您知道该怎么做,我也会接受它作为有效答案) .

更新:

我画了两张图片来说明我的问题。灰色区域是父线性布局。孩子们线性布局的绿色区域。红色区域是上面布局中注释的“其他视图”。

实际结果:

预期结果:

【问题讨论】:

  • 你能发布预期的输出图像吗? android:layout_weight 可以是一个解决方案。
  • 好的,我会这样做的
  • 你愿意使用约束布局,而不是线性布局吗?
  • 如果它有效,为什么不呢

标签: android view height


【解决方案1】:

通过以下调整,我实现了与您的第二张图片类似的布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_margin="20dp"
    android:background="@color/colorAccent">
    <!--Other views-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_margin="20dp"
    android:background="@color/colorAccent">
    <!--Other views-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
</LinearLayout>

【讨论】:

    【解决方案2】:

    你来了

    <?xml version="1.0" encoding="utf-8"?>
    <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="match_parent">
    
        <!--The linear layouts are only for representation.
        Constraint layout only hs direct children, baring a few.
        You can use packed chain for aligning the text views together in
        center vertical-->
    
        <LinearLayout
            android:id="@+id/left_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintBottom_toTopOf="@+id/barrier_demo"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/right_layout"
            app:layout_constraintTop_toTopOf="parent">
            <!--Other views-->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:textColor="#fff"
                android:background="@color/colorPrimary" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:background="@color/colorPrimary" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/right_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintLeft_toRightOf="@id/left_layout"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/barrier_demo"
            >
            <!--Other views-->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@color/colorPrimary" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@color/colorPrimary" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@color/colorPrimaryDark"
                android:textColor="#fff" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@color/colorPrimary"
    
                android:textColor="#fff" />
        </LinearLayout>
    
        <android.support.constraint.Barrier
            android:id="@+id/barrier_demo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="bottom"
            app:constraint_referenced_ids="left_layout,right_layout"
            />
    
    
    </android.support.constraint.ConstraintLayout>
    

    无论您在文本视图中放置多少文本,即使左侧的文本开始包含多行文本,也会调整正确的布局。 这是通过约束布局中的barrier来实现的。

    This 页面以一种很棒的方式解释它。 您应该避免嵌套在约束布局中的另一件事。 垂直居中对齐可以通过包链

    实现

    This中等文章是一个很好的来源。

    【讨论】:

    • 我的同事决定稍微改变实现,因为他不想花更多时间在这上面,但是我认为我提出的这种方法更好。所以我会在家里测试这个,如果它有效,我会接受你的回答
    猜你喜欢
    • 1970-01-01
    • 2015-08-24
    • 2013-07-10
    • 2019-08-10
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    相关资源
    最近更新 更多