Android 重力问题

发布于 2024-10-22 00:04:32 字数 568 浏览 2 评论 0原文

我一直在尝试将这个自定义视图置于扩展 LinearLayout 的另一个自定义视图的中心。这一切都需要通过代码完成,因为它都是在运行时完成的。

我尝试了设置重力的标准方法:

this.setGravity(Gravity.CENTER);

这是在扩展 LinearLayout 的类内部完成的。

我还尝试了 LayoutParams 方法:

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER;
    block.setLayoutParams(params);
    this.addView(block);

这是在我将块添加到视图的地方完成的(如您所见)。

这两种方法都会导致该块仍然在我的视图的左上角对齐。我还有什么其他选择,或者更重要的是,我到底做错了什么?

I've been trying to center this custom view I have inside this other custom View that extends LinearLayout. This all needs to be done via code because it's all done at runtime.

I've tried the standard approach with setting the gravity:

this.setGravity(Gravity.CENTER);

That was done inside of my class that extends LinearLayout.

I've also tried the LayoutParams method:

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER;
    block.setLayoutParams(params);
    this.addView(block);

This was done in the place where I add the block to the view (as you can see).

Both methods resulted in the block still being aligned in the upper-left hand corner of my view. What are my other options or, more importantly, what in the world am I doing wrong?

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

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

发布评论

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

评论(2

薄荷港 2024-10-29 00:04:32

还可以尝试设置布局权重:

LinearLayout.LayoutParams params =
    new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_HORIZONTAL;
params.weight = 1;
this.addView(block, params);

这(假设您的方向是垂直的)将允许您的视图填充线性布局中的剩余空间,并且应该将其水平居中。

我认为没有办法让视图子级小于包含它的 LinearLayout 单元格。如果视图小于其“单元格”,则 LinearLayout 将缩小以容纳它。如果布局权重导致“单元格”增长,那么包含的视图也会增长。

如果您确实希望视图小于其“单元格”并居中,请将其包装在 FrameLayout 中。然后你就可以随心所欲地使用重心了。
布局宽度=“填充父级”
在xml中(我知道你不能直接使用它,但这样解释起来更容易):

<LinearLayout orientation="vertical">
    <FrameLayout layout_weight="1" layout_height="wrap_content">
        <View layout_gravity="center" layout_{width,height}="wrap_content"/>
    </FrameLayout>
        ... other views ...
</LinearLayout>

未标记的布局属性是“fill_parent”。

Try also setting the layout weight:

LinearLayout.LayoutParams params =
    new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_HORIZONTAL;
params.weight = 1;
this.addView(block, params);

This (assuming your orientation is vertical) will allow your view to fill the remaining space in the linear layout and should center it horizontally.

I don't think there is a way to have a view child that is smaller than the LinearLayout cell that contains it. If the view is smaller than its "cell" then the LinearLayout will shrink to accomodate it. If the layout weight causes the "cell" to grow, then the contained view will grow as well.

If you really want the view to be smaller than its "cell" and centered, wrap it in a FrameLayout. Then you will be able to use a center gravity to your heart's content.
layout_width="fill_parent"
In xml (I know you can't use this directly, but it's easier to explain this way):

<LinearLayout orientation="vertical">
    <FrameLayout layout_weight="1" layout_height="wrap_content">
        <View layout_gravity="center" layout_{width,height}="wrap_content"/>
    </FrameLayout>
        ... other views ...
</LinearLayout>

Unmarked layout attributes are "fill_parent".

娇女薄笑 2024-10-29 00:04:32

看看这个: http: //thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/

基本上,android:gravity 会影响 View 中的位置,而 android:layout_gravity 会影响 View 中的位置。相对于其父母的视图。所以你需要使用布局重力来移动整个视图。

Take a look at this: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/

Basically, android:gravity affects positions things within a View, whereas android:layout_gravity positions a View relative to its parents. So you need to use layout gravity to move the whole View.

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