2016-11-30 70 views
1

有没有一种简单的方法来在android studio中创建如下所示的xml布局?我是iOS XCode的家伙,在使用比率约束的情况下使用它毫无问题。具有相同宽度和高度的视图

到目前为止,我使用ConstraintLayout和垂直gridlines百分比选项。但是这只给了我等宽的view1和view2,没有高度。

我知道重写onMeasure方法的解决方案,但我想避免它。

   a 
--------------------------- 
|       | 
|       | 
|       | 
|       | 
|  b   b  | 
| ----------- ----------- | 
| | view1 | | view2 | | 
| |b  | |b  | | 
| |   | |   | | 
| ----------- ----------- | 
--------------------------- 

b = 50% * a; 

回答

0

你可以做这样的事情:现在

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/imageview1" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 

    <ImageView 
     android:id="@+id/imageview2" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

</RelativeLayout> 

,在你的活动或片段,你会得到2次与findViewById();

然后:

ImageView view_instance1 = (ImageView)findViewById(R.id.imageView1); 
ImageView view_instance2 = (ImageView)findViewById(R.id.imageView2); 
     LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)view_instance1 .getLayoutParams(); 
     params.height= params.width; 
     view_instance1 .setLayoutParams(params); 
     view_instance2 .setLayoutParams(params); 
相关问题