【问题标题】:How make few views with same width如何制作几个具有相同宽度的视图
【发布时间】:2016-02-29 20:22:57
【问题描述】:

我有代码:

    <LinearLayout
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent">
            <ImageView
                android:gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent">
            <ImageView
                android:gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

当我尝试使用此代码时,左右布局的宽度不同。

我需要在行中创建两个具有相同宽度的 LinearLayout。

如果左侧布局具有最大宽度,则右侧布局必须将宽度更改为左侧宽度。如果右边的布局宽度最大,那么左边的布局必须改变宽度为右边的宽度。

父布局必须有 layout_width="wrap_content",而不是 "match_parent"。

我怎样才能使左右布局的尺寸相同?

【问题讨论】:

  • 我不明白为什么您的代码不起作用,但是,我有一个建议。 Google 有一个百分比支持库,它允许您以百分比设置维度。其他可能的解决方案是将您的代码转换为RelativeLayout,用空视图标记中心,而您的LinearLayouts 用于该空视图的开始和结束。
  • 当我更改父布局的属性 layout_width="match_parent" 我的代码工作正常并且两个线性布局具有相同的宽度,屏幕的 1/2。但是当父布局没有完整的屏幕宽度时,我需要没有'match_parent'的正确工作......

标签: java android xml android-linearlayout


【解决方案1】:
【解决方案2】:

在第一个LinearLayout 你错过了线路:

xmlns:android="http://schemas.android.com/apk/res/android"

添加这个就没有问题了。

【讨论】:

  • 我的 xml 开头有这一行,我不打印这一行以获得清晰的代码
  • 然后删除那个父 LinearLayout.
猜你喜欢
  • 2011-06-10
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多