[英]Android RelativeLayout sync width
我有一個LinearLayout
,其中包含其他兩個RelativeLayouts
(水平方向)。 我在運行時不知道左側RelativeLayout
的寬度。 是否可以同步兩個布局? 如果左側的RelativeLayout
的寬度為100dp ,則右側的RelativeLayout
的寬度也應為100dp 。 或兩者都應為50dp 。
我可以在XML文件中處理此問題,還是可以通過代碼並將LayoutParams
設置為相同的值來完成此工作?
在每個RelativeLayout中將寬度設置為0dp,將權重設置為1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
如果它是動態寬度,則沒有xml方式。 在xml中,您只能:
如果第一個布局的寬度未知,則需要使用一些技巧:
firstRelativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
secondRelativeLayout.getLayoutParams().width=firstRelativeLayout.width;
secondRelativeLayout.requestLayout();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.