在线性布局中对齐元素

问题描述 投票:0回答:2

我是android和学习的新手。我正在建立像UI和使用线性布局的形式。我试图将它们放在水平布局内的两个垂直线性布局中,但无法将标签文本元素与每个输入元素对齐。下面,我使用三种线性布局。

xml布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/linlaytop"
    android:layout_marginLeft="@dimen/linlayleft"
    android:layout_marginRight="@dimen/linlayleft"
    android:layout_marginBottom="@dimen/linlaytop"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/linlaytop">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/traffic"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
        <Spinner
            android:id="@+id/trafficSpinnerView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/linlayleft">
        </Spinner>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="@dimen/linlaytop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/location"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            />

        <EditText
            android:id="@+id/locationTxtView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:layout_marginLeft="@dimen/linlayleft"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="@dimen/linlaytop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/date"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
        <TextView
            android:id="@+id/dateTxtView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/datetime"
            android:layout_marginLeft="@dimen/linlayleft"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
    </LinearLayout>


</LinearLayout>

设计布局: enter image description here enter image description here

我想对齐这三个元素: enter image description here

请指教。

android layout android-linearlayout
2个回答
2
投票

将Weightsum赋予linearLayout,并为视图提供layout_weight(Textviews和spinner)。将视图宽度从wrap_content更改为0dp。对这三种布局都做同样的事情。希望这是你要找的。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100"
    android:layout_marginTop="@dimen/linlaytop">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:text="@string/traffic"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
    <Spinner
        android:id="@+id/trafficSpinnerView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="70"
        android:layout_marginLeft="@dimen/linlayleft">
    </Spinner>
</LinearLayout>

2
投票

干得好

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="10"
            android:layout_height="wrap_content"
            android:text="traffic"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
        <Spinner
            android:id="@+id/trafficSpinnerView"
            android:layout_width="0dp"
            android:layout_weight="40"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp">
        </Spinner>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="10"
            android:layout_height="wrap_content"
            android:text="location"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            />

        <EditText
            android:id="@+id/locationTxtView"
            android:layout_width="0dp"
            android:layout_weight="40"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="10"
            android:layout_height="wrap_content"
            android:text="date"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
        <TextView
            android:id="@+id/dateTxtView"
            android:layout_width="0dp"
            android:layout_weight="40"
            android:layout_height="wrap_content"
            android:hint="datetime"
            android:layout_marginLeft="10dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
    </LinearLayout>


</LinearLayout>

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.