[英]Android Layout: width half of parent
我有一個非常簡單的布局,我無法讓它看起來像我想要的。 它是一個帶有按鈕和開關的 LinearLayout。 我希望它們顯示一個在另一個之上,但我希望它們的寬度是父布局的一半。
|--LinearLayout---------|
| |
| ----------- |
|| Switch | |
| ----------- |
| ----------- |
|| button | |
| ----------- |
------------------------
我一直在查看 SO 中的其他類似答案,但找不到適合我的解決方案。 這是我迄今為止嘗試過的:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2" >
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/remember" />
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
</LinearLayout>
有了這個,按鈕和開關占據了父級的所有空間,而不是只占據一半。 我試過在孩子中使用android:layout_width = 0dp
,但它使它們消失了。
請問有什么幫助嗎?
一種可能的方法是擁有一個主水平 LinearLayout 將寬度拆分為 2,並在其內部具有垂直布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/remember" />
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
</LinearLayout>
<!-- Right side spacer -->
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
</LinearLayout>
訣竅是將0dp用於您要通過layout_weight管理的大小! 試試這個
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:weightSum="2" >
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="@string/remember" />
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
</LinearLayout>
</LinearLayout>
試試這個代碼
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/remember" />
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Button button = (Button) findViewById(R.id.share_button);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int buttonWidth = width/2;
將此buttonWidth
設置為您的按鈕,如button.setWidth(buttonWidth);
int params = linearLayout.getWidth()/2;
ViewGroup.LayoutParams( params, ViewGroup.LayoutParams.WRAP_CONTENT));
簡短的回答:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.