有关RelativeLayout布局中, 当一个控件被左右两个控件夹在中间时, 此控件如何靠近两个控件中的一个的问题?

来源:互联网 发布:最新淘宝人工在线客服 编辑:程序博客网 时间:2024/06/10 18:07
<RelativeLayout
        android:id="@+id/writemsg_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/title_background_style1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/message_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dip"
            android:background="@xml/messageback_button_style"
            />

        <TextView
            android:id="@+id/new_sms_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="6dip"
            android:layout_toRightOf="@id/message_back"
            android:text="@string/new_message"
            android:textSize="20sp"
            android:typeface="serif" />
        
        <View android:id="@+id/title_divider"
            android:layout_width="3dip"
            android:layout_height="0dip"
            android:layout_toRightOf="@id/new_sms_text"
            />

        <TextView
            android:id="@+id/right_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/left_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="3dip"
            android:gravity="right"
            android:layout_toRightOf="@id/title_divider"
            android:layout_toLeftOf="@id/right_text"
            android:ellipsize="end"
            android:singleLine="true" />

    </RelativeLayout>


我们看到红色部分的代码,android:layout_toRightOf="@id/title_divider"
            android:layout_toLeftOf="@id/right_text"
这两句话表示, 此TextView控件的左边是title_divider, 右边是right_text.那么如何将此控件靠近right_text呢, 我遇到这个情况啊,它始终靠近title_divider, 这样显示很不美观,最后发现啊, android:gravity="right"就用这句话就可以了。 不要小看这个问题哦, 我也是用了30分钟才解决,而且我认为一定会有人跟我遇到同样的问题。