【问题标题】:stretching a layout by widest child view and match_parent second child通过最宽的子视图和 match_parent 第二个子视图拉伸布局
【发布时间】:2018-12-21 23:22:56
【问题描述】:

我需要按最宽的子视图拉伸布局,并按父宽度匹配第二个视图。换句话说,结合 wrap_content 和 match_parent。

以下是 XML 示例(回收站视图中的项目):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">

    <TextView
      android:id="@+id/first"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@android:color/holo_blue_dark"
      android:text="Loooong"/>

    <TextView
      android:id="@+id/second"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@android:color/holo_red_light"
      android:text="Short"/>

</LinearLayout>

我尝试在 onMeasure() 方法中将 match_parent 设置为第二个视图,如果它的宽度小于第一个视图,但是当我滚动 recycler_view 时,一些视图被重置回 wrap_content。

我怎样才能实现这种行为?也许约束布局可以提供帮助?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    为什么不将两者都设置为match_parent

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_dark"
            android:text="Loooong"/>
    
        <TextView
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_light"
            android:text="Short"/>
    
    </LinearLayout>
    

    你不需要onMeasure()

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 2019-12-15
      相关资源
      最近更新 更多