简体   繁体   中英

android:layout_alignParentRight=“true” does nothing

I have an ImageView which I've set to android:layout_alignParentRight="true" however it does not appear on the right side of the screen and I'm not sure why (it is the white square appearing centered horizontally in the screenshot below).

I understand this can only be used with a RelativeLayout - but I've used one - so I'm not sure exactly why this is happening.

<?xml version="1.0" encoding="utf-8"?>
<com.example.project.DragLayer xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
    android:id="@+id/drag_layer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:weightSum="1.0" >

            <GridView
                android:id="@+id/image_grid_view"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="0.8"
                android:background="@color/grid_background"
                android:gravity="center"
                android:horizontalSpacing="2dip"
                android:numColumns="@integer/num_columns"
                android:stretchMode="columnWidth"
                android:verticalSpacing="2dip" />

            <RelativeLayout
                android:id="@+id/bottom_part"
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:layout_centerHorizontal="true"
                android:layout_weight="0.2"
                android:background="@android:color/black"
                android:orientation="horizontal"
                android:weightSum="1.0" >

                <Button
                    android:id="@+id/button_add_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:onClick="onClickAddImage"
                    android:text="Add image" />

                <com.example.project.DeleteZone
                    android:id="@+id/delete_zone_view"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:src="@drawable/delete_zone" />

                <FrameLayout
                    android:id="@+id/image_source_frame"
                    android:layout_width="fill_parent"
                    android:layout_height="80dp"
                    android:layout_weight="0.5" >

                    <RelativeLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_alignParentRight="true"
                        android:layout_gravity="right"
                        android:layout_marginRight="5dp"
                        android:layout_weight="1" >

                        <ImageView
                            android:id="@+id/sqwhite"
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            android:layout_alignParentRight="true"
                            android:layout_gravity="right"
                            android:layout_marginRight="5dp"
                            android:layout_weight="1" />

                        <EditText
                            android:id="@+id/editText1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_below="@id/sqwhite"
                            android:layout_marginRight="5dp" >

                            <requestFocus />
                        </EditText>

                        <TextView
                            android:id="@+id/textView1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_below="@id/editText1"
                            android:layout_marginRight="5dp"
                            android:text=""
                            android:textColor="@android:color/white" />
                    </RelativeLayout>
                </FrameLayout>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>

</com.example.project.DragLayer>

在此处输入图片说明

The one glaring thing that jumps out at me is the redefinition of the xmlns, which any compiler doesn't like and may prevent proper generation of the layout. When I removed that and some of your dead code and tried running this, the "sqwhite" appeared on the right side. See if fixing these items helps:

  • Your first LinearLayout redefines the XML Namespace, which is an error. This could be what's preventing the XML from compiling properly.

Other minor items:

  • layout_weight only applies to children of LinearLayout s, so having them in other ViewGroups is unnecessary. Remove them from image_source_frame and its children
  • Having bottom_part use layout_centerHorizontal in a vertical LinearLayout is redundant and should also be removed.
  • Having layout_alignParentRight in your RelativeLayout is useless, since only children of RelativeLayout s can use it. Please remove.
  • Maybe I'm missing it from the code you provided, but where is the white square actually coming from? You didn't set an android:src attribute to the ImageView . So is it possible that the actual sqwhite is in the right place, and that that white square represents something else?

EDIT: I've re-pasted your code here, with the items I listed removed. Maybe this will help:

<?xml version="1.0" encoding="utf-8"?>
<com.example.project.DragLayer xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drag_layer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:weightSum="1.0" >

            <GridView
                android:id="@+id/image_grid_view"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="0.8"
                android:background="@color/grid_background"
                android:gravity="center"
                android:horizontalSpacing="2dip"
                android:numColumns="@integer/num_columns"
                android:stretchMode="columnWidth"
                android:verticalSpacing="2dip" />

            <RelativeLayout
                android:id="@+id/bottom_part"
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:layout_weight="0.2"
                android:background="@android:color/black"
                android:weightSum="1.0" >

                <Button
                    android:id="@+id/button_add_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:onClick="onClickAddImage"
                    android:text="Add image" />

                <com.example.project.DeleteZone
                    android:id="@+id/delete_zone_view"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:src="@drawable/delete_zone" />

                <FrameLayout
                    android:id="@+id/image_source_frame"
                    android:layout_width="fill_parent"
                    android:layout_height="80dp" >

                    <RelativeLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="right"
                        android:layout_marginRight="5dp" >

                        <ImageView
                            android:id="@+id/sqwhite"
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            android:layout_alignParentRight="true"
                            android:layout_gravity="right"
                            android:layout_marginRight="5dp"
                            android:src="@android:drawable/ic_delete" /> <!-- for testing -->

                        <EditText
                            android:id="@+id/editText1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_below="@id/sqwhite"
                            android:layout_marginRight="5dp" />

                        <TextView
                            android:id="@+id/textView1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_below="@id/editText1"
                            android:layout_marginRight="5dp"
                            android:text=""
                            android:textColor="@android:color/white" />
                    </RelativeLayout>
                </FrameLayout>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</com.example.project.DragLayer>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM