【问题标题】:Aligning Elements in a Linear Layout在线性布局中对齐元素
【发布时间】:2018-03-01 19:32:27
【问题描述】:

我是 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>

设计布局:

我要对齐这三个元素:

请指教。

【问题讨论】:

  • I want to align these three elements - 按什么顺序?
  • 相互垂直对齐。
  • 在我看来,它们已经相互垂直对齐了。
  • 我的意思是每个元素的开始应该匹配相同的垂直边距。
  • 然后只为左边的所有元素分配一个静态宽度或使用宽度为0dp并添加layout_weight

标签: android layout android-linearlayout


【解决方案1】:

将 Weightsum 赋予您的 linearLayout,并将 layout_weight 赋予您的视图(Textviews 和 spinner)。将视图的宽度从 wrap_content 更改为 0dp。对所有三个这样的布局做同样的事情。 希望这就是您要找的。​​p>

<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>

【讨论】:

  • 谢谢!有效。解决方案是相应地设置layout_weight 属性以使元素在线性布局中对齐。
猜你喜欢
  • 1970-01-01
  • 2020-06-28
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 2014-10-24
  • 2011-12-02
  • 1970-01-01
相关资源
最近更新 更多