简体   繁体   English

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

[英]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). 我有一个ImageView ,我将其设置为android:layout_alignParentRight="true"但是它没有出现在屏幕的右侧,我不确定为什么(它是白色正方形,在下面的屏幕截图中居中显示) 。

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. 我知道这只能与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>

在此处输入图片说明

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. 让我惊讶的一件事是xmlns的重新定义,这是任何编译器都不喜欢的,可能会妨碍布局的正确生成。 When I removed that and some of your dead code and tried running this, the "sqwhite" appeared on the right side. 当我删除该代码以及一些无效代码并尝试运行此代码时,“ sqwhite”出现在右侧。 See if fixing these items helps: 看看解决这些问题是否有帮助:

  • Your first LinearLayout redefines the XML Namespace, which is an error. 您的第一个LinearLayout重新定义XML命名空间,这是一个错误。 This could be what's preventing the XML from compiling properly. 这可能是阻止XML正确编译的原因。

Other minor items: 其他次要项目:

  • layout_weight only applies to children of LinearLayout s, so having them in other ViewGroups is unnecessary. layout_weight仅适用于LinearLayout的子级,因此不需要将它们包含在其他ViewGroup中。 Remove them from image_source_frame and its children image_source_frame及其子项中删除它们
  • Having bottom_part use layout_centerHorizontal in a vertical LinearLayout is redundant and should also be removed. 在垂直的LinearLayout使用bottom_part使用layout_centerHorizontal是多余的,也应删除。
  • Having layout_alignParentRight in your RelativeLayout is useless, since only children of RelativeLayout s can use it. layout_alignParentRight在你RelativeLayout是没用的,因为只有孩子RelativeLayout S能够使用它。 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 . 您没有为ImageView设置android:src属性。 So is it possible that the actual sqwhite is in the right place, and that that white square represents something else? 那么,实际的sqwhite是否在正确的位置,而该白色正方形代表其他内容呢?

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android:layout_alignParentRight =“true”不起作用 - android:layout_alignParentRight=“true”doesn't work android:layout_alignParentRight =“ true”-editText仍出现在布局的左侧-Android / XML - android:layout_alignParentRight=“true” - editText still appears on the left of the layout - Android / XML 如何在Xamarin中以编程方式为android中的布局设置android:layout_alignParentRight =“ true”? - How can set android:layout_alignParentRight=“true” programmatically for a layout in android in xamarin? 按钮layout_alignParentRight在第一个循环上不起作用吗? - Button layout_alignParentRight does not work on the first loop? 如何通过java代码更改android:layout_alignParentRight运行时 - How to change android:layout_alignParentRight runtime by java code “ Layout_alignParentRight”和“ Layout_alignParentBottom” - “Layout_alignParentRight” and “Layout_alignParentBottom” 为什么我不能使用layout_toRightOf放置视图,而layout_alignParentRight =“ true” - Why I can't place view with layout_toRightOf the right of view with layout_alignParentRight=“true” Layout_alignParentRight 和其他人不工作 - Layout_alignParentRight and others aren't working 在Android 4.1.2中,layout_centerInParent会覆盖layout_alignParentRight / layout_alignParentLeft - layout_centerInParent overrides layout_alignParentRight/layout_alignParentLeft in Android 4.1.2 layout_alignParentRight或layout_alignParentLeft是否适用于包含布局? - Are layout_alignParentRight or layout_alignParentLeft work for include layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM