【问题标题】:ViewGroup set all item width to maximum required by the childViewGroup 将所有项目宽度设置为孩子要求的最大值
【发布时间】:2019-07-30 08:20:14
【问题描述】:

我想根据显示最长子视图所需的最大宽度,将我的 Linerlayout 中存在的所有孩子设置为具有相等的宽度。

【问题讨论】:

    标签: android android-viewgroup


    【解决方案1】:

    将 LinearLayout 的宽度设为 wrap_content,将所有子元素的宽度设为 match_parent。

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

    【讨论】:

      【解决方案2】:

      将父LinearLayout及其所有子项的宽度设置为ma​​tch_parent

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-18
        • 2018-05-22
        • 1970-01-01
        • 2020-02-24
        • 2018-06-08
        • 2019-10-02
        • 2018-10-19
        • 1970-01-01
        相关资源
        最近更新 更多