简体   繁体   English

定义layout_weight为水平?

[英]Defining layout_weight as horizontal?

I am trying to use android:layout_weight but I can't. 我正在尝试使用android:layout_weight但不能。 I use linearlayout and I use vertical orientation because when I use horizontal every element goes on one line. 我使用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"
     >

When I use vertical android assign weight az vertically. 当我使用垂直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"
         />

Look at this (sorry I haven't enough reputation). 这个 (对不起,我没有足够的声誉)。

The thing that I want to do is putting two edittext elements on same line. 我要做的是将两个edittext元素放在同一行上。 How to do that? 怎么做?

将布局方向更改为“ 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>

Try putting the EditText(s) in a new LinearLayout to manage the hierarchy better. 尝试将EditText放入新的LinearLayout中,以更好地管理层次结构。 Also set the width as 0dp so the layout_weight works correctly. 还要将宽度设置为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>

for Horizontal Layout use android:orientation="horizontal" in you LinearLayout with : 对于水平布局,请在您的LinearLayout use android:orientation="horizontal"与:

    android:layout_width="0dp"
    android:layout_weight="1"

for Vertical Layout use android:orientation="vertical" in you LinearLayout with : 对于Vertical Layout,请在LinearLayout use android:orientation="vertical"与:

    android:layout_height="0dp"
    android:layout_weight="1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM