为什么alignParentRight不起作用?

时间:2013-04-09 13:52:15

标签: android android-layout layout textview relativelayout

我是Android开发的菜鸟,我在构建布局时遇到了问题。我有一个包含textview和包含两个复选框的线性布局的相对布局。我希望textView显示在左侧,线性布局显示在线性布局的右侧与边缘齐平。目前,textview和linearlayout显示在彼此之上。非常感谢任何帮助。

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:text="Rotated Shelf:           " 
                android:layout_gravity="center_vertical"/>

            <LinearLayout
                android:id="@+id/rotated"
                android:layout_width="match_parent"
                android:layout_height="32dp"                   
                android:layout_alignParentRight="true">                  

                <CheckBox
                    android:id="@+id/rotatedshelfYES"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Yes"
                    android:layout_toLeftOf="@+id/rotatedshelfNO"
                     />

                <CheckBox
                    android:id="@+id/rotatedshelfNO"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingRight="75dp"
                    android:text="No"
                    android:layout_alignParentRight="true" />
            </LinearLayout>
        </RelativeLayout>

4 个答案:

答案 0 :(得分:1)

尝试将textview写在linear-layout并设置android:layout_weight=""它很好用

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/rotated"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:layout_alignParentRight="true" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight=".5"
            android:paddingLeft="5dp"
            android:text="Rotated Shelf:           " />

        <CheckBox
            android:id="@+id/rotatedshelfYES"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Yes" />

        <CheckBox
            android:id="@+id/rotatedshelfNO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="No" />
    </LinearLayout>
</RelativeLayout>

答案 1 :(得分:0)

<{1>}添加LinearLayout

上的

答案 2 :(得分:0)

如果要将TextView的右边缘与LinearLayout的左边缘对齐(即LinearLayout左侧的TextView),则应使用

android:layout_toLeftOf="@+id/rotated"
属性中的

答案 3 :(得分:0)

使用你必须在线性布局中设置android:layout_width等于wrap_content而不是match_parent,因为当我们想要等于宽度/高度作为小部件/容器的父级时使用match_parent,并且如名称所示,wrap_content只需要空间来写视图/容器的标题,不需要额外的空间。