如何将项目对齐到其父级中心的右侧? (android布局)

时间:2013-09-11 08:03:02

标签: android android-layout relativelayout textview

我想要这样的事情:
enter image description here

我用android relativelayout和它的标签如下:android:layout_centerHorizo​​ntal =“true”,这给了我这个:

enter image description here

我有其他标签的玩家并将它们合并,但没有成功。

有没有办法做到这一点?

4 个答案:

答案 0 :(得分:10)

试试这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="20dp"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:background="@android:color/black" >
</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/linearLayout1"
    android:text="YOUR TEXT" />

</RelativeLayout>

答案 1 :(得分:3)

试试这个

<RelativeLayout 
        android:layout_width="fill_parent"
        android:background="#FFFFFF"
        android:layout_height="wrap_content">


        <View 
            android:layout_width="10dp"
            android:layout_height="1dp"
            android:layout_centerInParent="true"
            android:id="@+id/dummy"
            />
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SDADASd"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/dummy"
            android:textColor="#000000"

            />
    </RelativeLayout>

答案 2 :(得分:2)

      or go with linearlayout...
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="right"
        android:layout_weight="1" >
        <View 
            android:layout_width="10dp"
            android:background="@android:color/black"
            android:layout_height="match_parent"/>
    </LinearLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/linearLayout1"
        android:text="YOUR TEXT" />
</LinearLayout>

答案 3 :(得分:1)

在RelativeLayout的中心添加透明视图,并使用

height="match_parent"
width="15dp"//(it doesn't matter much)

然后给出相对于此视图的布局参数,如

android:layout_alignRight="@+id/yourView"