Java 如何创建几个宽度相同的视图

Java 如何创建几个宽度相同的视图,java,android,xml,android-linearlayout,Java,Android,Xml,Android Linearlayout,我有密码: <LinearLayout android:orientation="horizontal" android:weightSum="2" android:layout_width="wrap_content" android:layout_height="match_parent"> <LinearLayout android:orientation="v

我有密码:

    <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>

当我尝试使用这段代码时,左右布局的宽度是不同的

我需要在同一行中创建两个宽度相同的线性布局

若左侧布局具有最大宽度,则右侧布局必须将宽度更改为左侧宽度。若右布局具有最大宽度,则左布局必须将宽度更改为右宽度

父版面必须具有版面宽度=“包装内容”,而不是“匹配父版面”


如何使左右布局的大小相同?

在第一个
线性布局上使用[Percentange Relative Layout][1]

您漏了行:

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

添加此选项将不会有任何问题。

我不明白为什么您的代码不起作用,但我有一个建议。谷歌有一个百分比支持库,允许你用百分比设置维度。其他可能的解决方案是将代码转换为RelativeLayout,以空视图标记中心,将线性布局转换为空视图的开始和结束。当我更改父布局的属性layout\u width=“match\u parent”时,我的代码工作正常,两个线性布局的宽度相同,为屏幕的1/2。但我需要在没有“匹配父项”的情况下进行正确的工作,当父项布局没有全屏幕宽度时…我的xml头部有这一行,我不打印这一行以获得清晰的代码,然后删除该父项
LinearLayout