Android-将视图中心与其他视图的底部对齐 [英] Android - Align view center to bottom of other view
问题描述
图片不仅仅代表冗长的演讲:
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...
感谢您的帮助!
推荐答案
最后,我使用一种更具编程性的方法来解决此问题,因为视图的大小不是固定的。
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屋!