Android 在线性布局中对齐图元
我对android和学习都是新手。我正在构建类似UI的表单,并使用线性布局。我曾尝试将它们放在水平布局中的两个垂直线性布局中,但无法将标签文本元素与每个输入元素对齐。下面,我使用三个线性布局 xml布局:Android 在线性布局中对齐图元,android,layout,android-linearlayout,Android,Layout,Android Linearlayout,我对android和学习都是新手。我正在构建类似UI的表单,并使用线性布局。我曾尝试将它们放在水平布局中的两个垂直线性布局中,但无法将标签文本元素与每个输入元素对齐。下面,我使用三个线性布局 xml布局: <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="@dimen/linlaytop"
<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>
设计布局:我想对齐这三个元素:
请告知。为线性布局指定权重总和,并为视图(文本视图和微调器)指定布局权重。将视图宽度从“包裹内容”更改为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>
为线性布局赋予权重总和,并为视图(文本视图和微调器)赋予布局权重。将视图宽度从“包裹内容”更改为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>
给你
<?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>
给你
<?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>
我想对齐这三个元素
-以什么顺序?垂直对齐。我看到它们已经垂直对齐了。我的意思是每个元素的开头应该匹配相同的垂直边距。然后只需为左侧的所有元素指定一个静态宽度,或者使用宽度作为0dp并添加layout\u weight
我想将这三个元素按什么顺序对齐?垂直对齐。我看到它们已经垂直对齐了。我的意思是每个元素的开头应该匹配相同的垂直边距。然后只需为左侧的所有元素指定一个静态宽度,或者使用宽度作为0dp并添加layout\u weight
谢谢!成功了。解决方案是相应地设置layout\u weight
属性,以使元素在线性布局中对齐。谢谢!成功了。解决方案是相应地设置layout\u weight
属性,以使元素在线性布局中对齐。请始终是我的:)快乐编码:)请始终是我的:)快乐编码:)