簡體   English   中英

android:layout_alignParentRight =“ true”什么都不做

[英]android:layout_alignParentRight=“true” does nothing

我有一個ImageView ,我將其設置為android:layout_alignParentRight="true"但是它沒有出現在屏幕的右側,我不確定為什么(它是白色正方形,在下面的屏幕截圖中居中顯示) 。

我知道這只能與RelativeLayout一起使用-但我已經使用了一個-所以我不確定為什么會這樣。

<?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>

在此處輸入圖片說明

讓我驚訝的一件事是xmlns的重新定義,這是任何編譯器都不喜歡的,可能會妨礙布局的正確生成。 當我刪除該代碼以及一些無效代碼並嘗試運行此代碼時,“ sqwhite”出現在右側。 看看解決這些問題是否有幫助:

  • 您的第一個LinearLayout重新定義XML命名空間,這是一個錯誤。 這可能是阻止XML正確編譯的原因。

其他次要項目:

  • layout_weight僅適用於LinearLayout的子級,因此不需要將它們包含在其他ViewGroup中。 image_source_frame及其子項中刪除它們
  • 在垂直的LinearLayout使用bottom_part使用layout_centerHorizontal是多余的,也應刪除。
  • layout_alignParentRight在你RelativeLayout是沒用的,因為只有孩子RelativeLayout S能夠使用它。 請刪除。
  • 也許您提供的代碼中缺少它,但是白色方塊實際上是哪里來的? 您沒有為ImageView設置android:src屬性。 那么,實際的sqwhite是否在正確的位置,而該白色正方形代表其他內容呢?

編輯:我在這里重新粘貼了您的代碼,刪除了我列出的項目。 也許這會有所幫助:

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM