当前位置: 动力学知识库 > 问答 > 编程问答 >

Android RelativeLayout layout_toLeftOf doesn't work as expected

问题描述:

<?xml version="1.0" encoding="utf-8"?>

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#cccccc">

<!-- LEFT -->

<FrameLayout

android:layout_marginTop="50px"

android:layout_marginLeft="50px"

android:layout_width="50px"

android:layout_height="50px"

android:background="#ff0000"

android:id="@+id/lp" />

<FrameLayout

android:layout_width="50px"

android:layout_height="100px"

android:background="#00ff00"

android:layout_toRightOf="@id/lp"

android:layout_alignTop="@id/lp"

android:id="@+id/lc" />

<!-- END LEFT -->

<!-- RIGHT -->

<FrameLayout

android:layout_marginTop="50px"

android:layout_marginLeft="300px"

android:layout_width="50px"

android:layout_height="50px"

android:background="#ff0000"

android:id="@+id/rp" />

<FrameLayout

android:layout_width="50px"

android:layout_height="100px"

android:background="#00ff00"

android:layout_toLeftOf="@id/rp"

android:layout_alignTop="@id/rp"

android:id="@+id/rc" />

<!-- END LEFT -->

</RelativeLayout>

The result is:

What I expect is:

Why doesn't it work?

网友答案:

Here is my answer to get it working:

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

     <!-- LEFT -->
    <View
        android:layout_alignParentLeft="true"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="50dp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#ff0000"
        android:id="@+id/lp" />

    <View 
        android:layout_width="50dp" 
        android:layout_height="100dp" 
        android:background="#00ff00"
        android:layout_toRightOf="@id/lp"
        android:layout_alignTop="@id/lp"
        android:id="@+id/lc" />        
    <!-- END LEFT -->

    <!-- RIGHT -->
    <View
        android:layout_alignParentRight="true" 
        android:layout_marginTop="50dp"
        android:layout_marginRight="50dp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#ff0000"
        android:id="@+id/rp" />

    <View 
        android:layout_width="50dp" 
        android:layout_height="100dp" 
        android:background="#00ff00"
        android:layout_toLeftOf="@id/rp"
        android:layout_alignTop="@id/rp"
        android:id="@+id/rc" />        
    <!-- END LEFT -->    
</RelativeLayout>

Do not use px. Use dip/dp (independent pixels). Also to create your coloured blocks you only need a View, however it depends what is going in them.

Read this tutorial I found it really useful for layouts - http://www.learn-android.com/2010/01/05/android-layout-tutorial/5/

网友答案:

from android:layout_toLeftOf definition: Positions the right edge of this view to the left of the given anchor view ID. Accommodates right margin of this view and left margin of anchor view.

So, the magic is put the dummy View without margin:

  <View 
    android:id="@+id/ltEdge"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:layout_alignLeft="@id/lp"        
      />

  <View
    android:layout_width="50px" 
    android:layout_height="100px" 
    android:background="#00ff00"
    android:layout_toRightOf="@id/ltEdge"
    android:layout_alignTop="@id/lp"
    android:id="@+id/lc" /> 
分享给朋友:
您可能感兴趣的文章:
随机阅读: