Cuối cùng, tôi sử dụng một cách có lập trình hơn để giải quyết vấn đề này, vì kích thước Chế độ xem không cố định.
Dưới đây là giải pháp:
Cách bố trí:
<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>
Mã:
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);
}
});
Nhờ sự giúp đỡ của bạn! Nó giúp tôi tìm thấy điều này!
Sử dụng FrameLayout làm cha mẹ và sau đó đặt trọng lực ở dưới cùng. Ở trẻ đặt lề trên cho chiều cao con/2 – vilpe89