2016-02-29 89 views
1

几点看法我有一些代码:如何使具有相同的宽度

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

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

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

如果左侧布局的宽度最大,则右侧布局必须将宽度更改为左侧宽度。如果右侧布局的宽度最大,则左侧布局必须将宽度更改为右侧宽度。

父级布局必须具有layout_width =“wrap_content”,而不是“match_parent”。

我怎样才能做左右版面尺寸相同?

+0

我不明白为什么你的代码不起作用,但是,我有一个建议。 Google提供的百分比支持库允许您按百分比设置维度。其他可能的解决方案是将代码转换为RelativeLayout,其中空视图用于标记中心,LinearLayouts用于开始和结束空视图。 – Marius

+0

当我改变父布局的属性layout_width =“match_parent”我的代码工作正确,两个linearlayouts具有相同的宽度,1/2的屏幕。但是,我不需要'match_parent',当父级布局没有全屏幕时,我需要正确的工作... –

回答

0

在第一LinearLayout你已经错过了行:

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

添加这一点,就不会有问题。

+0

我有这条线在我的xml的头上,我不打印这行清除代码 –

+0

然后删除该父'LinearLayout'。 – Stanojkovic

相关问题