<RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="#FF9E3E"
    >
     <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dip"
    android:text="用户注册"
    android:textColor="#000000"
     />
     <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
  android:layout_marginRight="20dip"
    android:text="用户登录"
    android:textColor="#000000"
     />
</RelativeLayout>我想实现“用户注册”离屏幕左边20dip的距离,而“用户登录”离屏幕右边20dip的距离,现在不知怎么“用户登录”跑到屏幕左边顶头来了,注:现在只能用RelativeLayout

解决方案 »

  1.   

    俺对LAYOUT不熟啊。。
    只能帮你顶了。。
      

  2.   

    看了下sdk,貌似这个 android:layout_marginRight="20dip"
    是指该控件的右边与屏幕左边距的距离,而不是我先认为的控件的右边与屏幕右边距的距离那我如果想实现我想达到的效果,就得用android:layout_marginRight=屏幕的宽度-20dip
    这个应该怎么写啊
      

  3.   

    file:///E:/android-sdk-windows-1.5_r1/docs/guide/topics/ui/layout-objects.html
    RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center. Also, because of this ordering, if using XML to specify this layout, the element that you will reference (in order to position other view objects) must be listed in the XML file before you refer to it from the other views via its reference ID.The example below shows an XML file and the resulting screen in the UI. Note that the attributes that refer to relative elements (e.g., layout_toLeft) refer to the ID using the syntax of a relative resource (@id/id).<?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="wrap_content"
                    android:background="@drawable/blue"
                    android:padding="10px" >    <TextView android:id="@+id/label" 
                  android:layout_width="fill_parent" 
                  android:layout_height="wrap_content" 
                  android:text="Type here:" />    <EditText android:id="@+id/entry" 
                  android:layout_width="fill_parent" 
                  android:layout_height="wrap_content" 
                  android:background="@android:drawable/editbox_background"
                  android:layout_below="@id/label" />
      
        <Button android:id="@+id/ok" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_below="@id/entry"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="10px"
                android:text="OK" />    <Button android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@id/ok"
                android:layout_alignTop="@id/ok"
                android:text="Cancel" />
    </RelativeLayout>Some of these properties are supported directly by the element, and some are supported by its LayoutParams member (subclass RelativeLayout for all the elements in this screen, because all elements are children of a RelativeLayout parent object). The defined RelativeLayout parameters are: width, height, below, alignTop, toLeft, padding[Bottom|Left|Right|Top], and margin[Bottom|Left|Right|Top]. Note that some of these parameters specifically support relative layout positions — their values must be the ID of the element to which you'd like this view laid relative. For example, assigning the parameter toLeft="my_button" to a TextView would place the TextView to the left of the View with the ID my_button (which must be written in the XML before the TextView).To view this sample code, see the Hello RelativeLayout tutorial.
      

  4.   

    你需要为textview添加id, 然后设置textview之间的相对位置关系买本教程吧,少走些弯路
      

  5.   

    我有电子书,我现在不是要设置textview之间的位置,而是要设置textview和屏幕之间的位置
      

  6.   

    Layout above/Layout below、Layout to left of。等同样用法
      

  7.   

    <TextView  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_marginRight="20dip"
                android:text="用户登录"
                android:textColor="#000000"
                android:layout_alignParentRight="true"
            />这个TV加入android:layout_alignParentRight="true",表示对于父类view右边齐.
      

  8.   

    <?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="wrap_content"
            android:background="#FF9E3E"
        >
            <TextView  
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dip"
                android:text="用户注册"
            />
            <TextView  
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                 android:layout_marginRight="20dip"
                android:text="用户登录"
                android:gravity="right"
            />
    </RelativeLayout>
    这样也可以
      

  9.   

    用android:layout_marginLeft和android:layout_marginRight属性
      

  10.   

    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#FF9E3E"
        >
            <TextView  
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dip"
                android:layout_marginLeft="右边距离默认到右边头"
                android:text="用户注册"
                android:textColor="#000000"
            />
            <TextView  
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                 android:layout_marginRight="20dip"
                 android:layout_marginLeft="左边距离默认到左边头"            android:text="用户登录"
                android:textColor="#000000"
            />
    </RelativeLayout>
      

  11.   

     <RelativeLayout
         android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#FF9E3E"
        >
            <TextView  
             android:id="@+id/textview_1"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dip"
                android:text="用户注册"
                android:textColor="#000000"
            />
            <TextView  
            android:id="@+id/textview_2"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                 android:layout_marginRight="20dip"
                android:text="用户登录"
                android:textColor="#000000"
            />
    </RelativeLayout>