为什么我的相对布局占据全屏宽度

发布于 2024-12-09 08:19:27 字数 1327 浏览 6 评论 0原文

为什么我的相对布局占据全屏宽度

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#f00"
    >
    <Button  
        android:id="@+id/Button01"  
        android:text="Press Here"  
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"></Button> 
     <Button  
        android:id="@+id/Button02"  
        android:text="02"  
        android:layout_alignParentLeft="true" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"></Button>  
</RelativeLayout>

我已经将相对布局指定为“wrap_content”,那么为什么它会占据全屏空间。即使我说 android:layout_width="fill_parent" 输出也是相同的。 Enlighten

请赐教!

编辑:我想我之前的问题不是很清楚。对此表示歉意。 当我在相对布局中有 2 个子视图,其中一个与父视图左对齐,另一个右对齐且相对布局宽度为 WRAP_CONTENT 时,我期望布局宽度只是 2 个按钮宽度的总和(不是这就是 WRAP_CONTENT 的意思??)。我知道还有其他方法可以实现我正在寻找的 UI,但我只是想正确理解这些相对布局标签。

编辑2:我已经进行了一些实验,看起来如果我们使用 Layout_AlighParentRight 及其父级的宽度作为 WRAP_CONTENT ,那么上部布局宽度将用于计算(就像下面指出的几个答案)。但我们只使用 Layout_alignParentLeft 那么它会按预期工作,并且布局宽度不会扩展到整个屏幕。感谢各位的帮助!

Why does my relative layout occupy full screen width

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#f00"
    >
    <Button  
        android:id="@+id/Button01"  
        android:text="Press Here"  
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"></Button> 
     <Button  
        android:id="@+id/Button02"  
        android:text="02"  
        android:layout_alignParentLeft="true" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"></Button>  
</RelativeLayout>

I have specified the relative layout to "wrap_content", then why does it occupy full screen space. Out put is same even if i say android:layout_width="fill_parent". Enlighten

Enlighten me please!

EDIT : I think i was not very clear with my question earlier. Apologies for that.
When I have 2 child views in a relative layout and one of them is left aligned to parent and other is right aligned and relative layouts width is WRAP_CONTENT then I expected the layouts width to be just the sum of width of 2 buttons (isn't that's what WRAP_CONTENT means??). I know there are other ways of achieving the UI im looking for but Im just trying to understand these relative layout tags properly.

EDIT 2: I have experimented a bit and it looks like if we using Layout_AlighParentRight with its parent's width as WRAP_CONTENT then the upper layout width is used for calculation (like few answers pointed out below). But we are using just the Layout_alignParentLeft then it works as expected and layout width is not extending to the complete screen. Thanks for the help folks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲鸠 2024-12-16 08:19:27

其他答案已正确指出,当相对布局的宽度设置为 wrap_content 且其子布局向左和向右对齐时,相对布局采用其父布局的宽度 - 在这种情况下,整个屏幕。但是,如果两个子项都向一侧对齐,则相对布局将与最宽的子项一样宽。

现在,如果您希望两个按钮彼此相邻放置,并且相对布局与按钮宽度之和一样宽,则需要稍微不同的方法。不要相对于父级定位两个按钮,而只需使用一个按钮(例如第一个按钮)即可。假设它的定位保持不变 (android:layout_alignParentRight="true")。现在按钮向右浮动,因此第二个按钮为了位于它旁边,必须与第一个按钮的左侧对齐。因此,我们只需添加 android:layout_toLeftOf="@id/Button01" (并删除 android:layout_alignParentLeft="true" 部分)。

有关更多信息,我建议您查看有关相对布局的非常友好的教程。

The other answers have correctly pointed out that when your relative layout's width is set to wrap_content, and its children are aligned to both left and right, the relative layout takes the width of its parent - in this case, the entire screen. If, however, both children were aligned to one side, the relative layout would be as wide as the widest child.

Now if you want the two buttons to be placed next to each other, and the relative layout to be as wide as the sum of the widths of the buttons, a slightly different approach is needed. Instead of positioning both buttons relative to the parent, do that with one button only (e.g, the first one). Let's say its positioning is left unchanged (android:layout_alignParentRight="true"). Now the button is floated to the right, so the second button, in order to be position next to it, has to be aligned to the first button's left side. Thus, we just add android:layout_toLeftOf="@id/Button01" (and remove the android:layout_alignParentLeft="true" part).

For more, I suggest you check out a very friendly tutorial on relative layouts.

淡墨 2024-12-16 08:19:27

因为您有一个

        android:layout_alignParentLeft="true" 

对象的宽度和

        android:layout_alignParentRight="true" 

另一个对象的宽度,然后布局延伸到两侧,为您提供全宽度布局。

但是,当您使用 Layout_alignParentXXXXX 并放入父级 WRAP_CONTENT 时,这会使子级转到定义了宽度的上部布局。

cause you have a

        android:layout_alignParentLeft="true" 

width an object , and a

        android:layout_alignParentRight="true" 

width another object , then the layout extends to both side , giving you the full width layout.

But when you use Layout_alignParentXXXXX , and you put in parent WRAP_CONTENT , that makes children to go to the upper layout with a width defined.

枕花眠 2024-12-16 08:19:27

此行使“按此处”按钮 (Button01) 向右对齐:

android:layout_alignParentRight="true" 

这使您的布局填充父级的宽度。

This line makes the "Press Here" button (Button01) align to the right:

android:layout_alignParentRight="true" 

That makes your layout fill the parent in width.

客…行舟 2024-12-16 08:19:27

您可能面临的另一个问题是,如果您将 2 个子级设置为“对齐父级右”+“wrap_content”,并将相对布局设置为“wrap_content”,并且该相对布局包含在全屏 LinearLayout 中,您的相对布局将占用整个 LinearLayout 宽度。

如果您同时使用“向左对齐父级”,则相对布局会粘在左侧,并且其宽度实际上是“换行内容”。但“将父级向右对齐”的行为有所不同,这有点奇怪。

解决方法:

为了解决这个问题(我必须将其中一个子元素向右对齐),我实际上将 2 个子元素设置为“向左对齐父元素”,并使用子元素填充以获得其中之一孩子们位于右上角。这是一种肮脏的解决方法,但目前是我找到的唯一方法。

可能的更简洁的解决方案:

另一个技巧是将 2 个 LinearLayout 放入 FrameLayout 中,然后将真正的子级放入每个 LinearLayout 中,并利用这些 LinearLayout 的重力将子级定位在正确的位置。

  • 相对布局
    • 线性布局
      • 子级 1 (wrap_content)
    • LinearLayout(重力:右上角)
      • 子级 2 (wrap_content)

Another issue you may face is that if you set your 2 children to "align parent right"+"wrap_content", and your relative layout to "wrap_content", and that relative layout is contained in a full screen LinearLayout your relative layout will occupy the whole LinearLayout width.

If you do that with both "align parent left", the relative layout sticks on the left, and its width is really a "wrap content". But the behaviour is different for "align parent right", that's a bit strange.

Workaround:

To solve that issue (I had to align one of the children to the right), I actually set the 2 children to "align parent left" and played with children padding in order to get one of the children positionned on the top right corner. This is a dirty workaround but the only one i've found for now.

Possible cleaner solutions:

Another trick would be to put 2 LinearLayout inside a FrameLayout, then put your real children in each LinearLayout, and play with the gravity of those LinearLayout to position children at the right position.

  • RelativeLayout
    • LinearLayout
      • Child 1 (wrap_content)
    • LinearLayout (gravity: top right)
      • Child 2 (wrap_content)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文