Android - 将视图中心与其他视图的底部对齐 [英] Android - Align view center to bottom of other view

查看:30
本文介绍了Android - 将视图中心与其他视图的底部对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一张图片比一个冗长的演讲更能说明问题:

A picture tells more than a lengthy speech :

我想将红色部分的中心与黑色部分的中间垂直对齐.我没有容器限制(RelativeLayout、FrameLayout、LinearLayout oO).

I want to align vertically the center of the red part with the middle of the black part. I have no constraint of container (RelativeLayout, FrameLayout, LinearLayout oO ).

我尝试了一个高度为 0dp 的视图与黑色视图的底部对齐,并将红色视图的 alignBaseline 与它对齐,但它不起作用......

I tried a View with height of 0dp aligned to the bottom of the black view and alignBaseline of red view to it, but it doesn't work...

感谢您的帮助!

推荐答案

最后我用了更程序化的方式来解决这个问题,因为Views的大小不是固定的.

Finally, I use a more programmatic way to solve this problem, because the size of Views are not fixed.

解决办法:

布局:

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <View
                android:id="@+id/black"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"/>

            <View
                android:id="@+id/red"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true">
        </RelativeLayout>

代码:

            red.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    red.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                    LayoutParams params = new LayoutParams(
                            LayoutParams.MATCH_PARENT,      
                            LayoutParams.WRAP_CONTENT
                            );
                    params.setMargins(0, 0, 0, red.getHeight()/2);
                    black.setLayoutParams(params);
                }
            });

感谢您的帮助!它帮助我找到了这个!

Thanks for your help ! It helps me found this !

这篇关于Android - 将视图中心与其他视图的底部对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆