2014-11-14 54 views
3

我有在Android的布局问题。我希望这两个视图应该具有相同的宽度,并且他们几乎应该使用屏幕的整个宽度。每个视图都应该包含一个居中标签。Android的布局 - 彼此相邻,宽度相等的两个观点,使用全屏幕宽度

它应该是这样的,当它这样做:

Example

这是我到目前为止有:

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" android:layout_height="match_parent"> 

     <View 
      android:id="@+id/view1" 
      android:layout_width="10dp" 
      android:layout_height="90dp" 
      android:layout_alignParentTop="true" 
      android:layout_marginTop="10dp" 
      android:layout_marginLeft="10dp" /> 

     <View 
      android:id="@+id/view2" 
      android:layout_width="10dp" 
      android:layout_height="90dp" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:layout_marginTop="10dp" 
      android:layout_marginRight="10dp" /> 

    </RelativeLayout> 

我只是占位符值的宽度现在。

谢谢。

+0

尝试使用的LinearLayout与方向= “横向” 和体重= 1两的观点。 – kha 2014-11-14 12:48:46

回答

9

你必须使用Android:在XML layout_weight属性,以便尝试下面的代码希望它能够解决您的问题: -

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button 
     android:text="Register" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:layout_margin="5dp" 
     android:layout_weight="1" /> 

    <Button 
     android:text="Not this time" 
     android:id="@+id/cancel" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:layout_margin="5dp" 
     android:layout_weight="1" /> 

    </LinearLayout> 
0

你必须使用layout_weight属性这一点。

 <Button 
     android:text="Register" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:layout_margin="5dp" 
     android:layout_weight="1" /> 

    <Button 
     android:text="Not this time" 
     android:id="@+id/cancel" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:layout_margin="5dp" 
     android:layout_weight="1" /> 

    </LinearLayout> 
相关问题