我对垂直线性布局中的重量感到困惑。我可以在水平线性布局中很好地使用重量,但如何在垂直线性布局中使用它。我有一个相对布局,其中包含一个衬里布局(垂直),有一些文本视图和文本字段。我想要以下安排。
红线=相对布局,绿线=线性布局(垂直),蓝色= textview,黄色= texfields
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:2)
如果您使用的是layout_weigth参数,则应该具有与match_parent上线性布局方向相关的参数
这样:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
答案 1 :(得分:1)
- 首先,您可以直接在Relative layout
中进行此安排,而不使用使用LinearLayout
- 其次,如果您想在Relative layout
中设置子布局,那么最好在TableRow
中使用Relative layout
,并排列元素就像你已经表现出来一样。
答案 2 :(得分:1)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 3 :(得分:0)
我建议您使用TableLayout
。如果我做对了,你就是在尝试水平加权,这样你就可以在LinearLayout
中找到常规列。假设是对的,那么你永远不会这样做。
LinearLayout
在其自身内水平或垂直排列所有元素。它无法双管齐下。所以在任何情况下你都需要一个单独的行容器来实现你的目标。
TableLayout
不仅设计了该内在的行容器,而且还为这种布局提供内置列支持,而无需加权来管理。
答案 4 :(得分:0)
在我的情况下,在垂直重量问题的小游戏之后,我明白了android会 环绕ViewGroup的内容,即使您使用比其内容大小更大的权重设置它,它也会环绕内容并为其他视图提供额外的空间。
解决方案是根据您的需要添加上边距或下边距。