![](/img/trans.png)
[英]Flexible Horizontal Layout with `layout_weight` and `maxWidth`
[英]Defining layout_weight as horizontal?
我正在嘗試使用android:layout_weight但不能。 我使用linearlayout並使用垂直方向,因為當我使用水平時,每個元素都在一行上。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
>
當我使用垂直android垂直分配權重az時。
<EditText
android:id="@+id/etFirstNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
/>
<EditText
android:id="@+id/etSecondNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
/>
看這個 (對不起,我沒有足夠的聲譽)。
我要做的是將兩個edittext元素放在同一行上。 怎么做?
將布局方向更改為“ Horizontal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
....
...
..
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10"
android:orientation="horizontal">
<EditText
android:id="@+id/etFirstNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5" />
<EditText
android:id="@+id/etSecondNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"/>
</LinearLayout>
<Button
....
...
..
</Button>
</LinearLayout>
嘗試將EditText放入新的LinearLayout中,以更好地管理層次結構。 還要將寬度設置為0dp,以便layout_weight
正常工作。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10"
android:orientation="horizontal">
<EditText
android:id="@+id/etFirstNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5" />
<EditText
android:id="@+id/etSecondNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"/>
</LinearLayout>
對於水平布局,請在您的LinearLayout
use android:orientation="horizontal"
與:
android:layout_width="0dp"
android:layout_weight="1"
對於Vertical Layout,請在LinearLayout
use android:orientation="vertical"
與:
android:layout_height="0dp"
android:layout_weight="1"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.