2015-06-19 71 views
0

我有以下xml实现。但是,正如您在下面看到的,高度和宽度是硬编码的,即使该值适用于Nexus 10.但它在Nexus 7上无法正常工作。相应地调整视图高度和宽度

我需要知道是否有办法处理它有点像百分比?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/mainView" 
    android:background="#0000FF"> 
<View 
     android:layout_width="220dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/view1" 
     android:background="#000000" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true" /> 
<View 
     android:layout_width="wrap_content" 
     android:layout_height="360dp" 
     android:id="@+id/view2" 
     android:background="#00FF0000" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" /> 
<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="44dp" 
     android:layout_below="@id/view2" 
     android:id="@+id/view3" 
     android:background="#000000" 
     android:layout_alignLeft="@id/view2" 
     android:layout_alignParentRight="true"> 
<View 
     android:layout_width="wrap_content" 
     android:layout_height="360dp" 
     android:layout_toRightOf="@id/view1" 
     android:id="@+id/view4" 
     android:background="#00FF0000" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout 

这里的布局设计

enter image description here

,我不想RelativeLayout的转换为线性布局,因为我对view2view 4顶部添加透明观点的理由接收手势事件。

+0

使用布局权重Linera称布局 – 3xplore

+0

我已经查看和线性布局。 – mystackoverflow

回答

1

要指定百分比,您需要以编程方式做,或者你可以在LinearLayout中包装它:

<LinearLayout 
    android:orientation="vertical"> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 
</LinearLayout> 

在上面的例子中,所有View s将从高度相等的LinearLayout。这意味着如果LinearLayout的高度是300dp,则每个View的高度将为100dp。

+0

非常感谢您的快速回答。实际上我有四个视图,其中一个是垂直站立('view1')。请看我编辑的问题。我已经提高了你的努力。除此之外,我想'view3'作为线性布局而不是查看。 – mystackoverflow

2

将ur整个布局更改为线性布局。现在增加重量作为需要。我没有增加weight.this只是一个示例代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mainView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal"> 

<View 
    android:layout_width="220dp" 
    android:layout_height="wrap_content" 
    android:background="#000000" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" 
     android:background="#ffffff" /> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" 
     android:background="#000000" /> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#bbbbbb" /> 


</LinearLayout> 

+0

我不想将它转换为线性布局的原因,因为我在“view2”和“view 4”的顶部添加了一个透明视图来接收手势事件。 – mystackoverflow

相关问题