如何在 Android 中对齐两个视图?
How to align two views in Android?
我在线性布局中有两个视图。一种视图是 TextView,另一种是 EditText。我想让他们两个都对齐。这是截图和代码。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ccc"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:text="S1"
android:background="#110faa">
</TextView>
<EditText
android:layout_width="80dp"
android:layout_height="20dp"
android:background="#102df2"
android:inputType="numberDecimal"
android:importantForAutofill="no">
</EditText>
</LinearLayout>
这就是现在的样子我不希望 S1 文本视图上有顶部间距。
截图如下:
使用android:layout_weight
分割LinearLayout的宽度和
android:baselineAligned="false"
顶部间距
<LinearLayout android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ccc"
android:orientation="horizontal"
android:layout_weight="5"
android:baselineAligned="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_weight="1"
android:background="#110faa"
android:text="S1">
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_weight="4"
android:background="#102df2"
android:importantForAutofill="no"
android:inputType="numberDecimal"></EditText>
</LinearLayout>
你可以这样做:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ccc"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="S1"
android:background="#110faa">
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:background="#102df2"
android:inputType="numberDecimal"
android:importantForAutofill="no"/>
</LinearLayout>
我在线性布局中有两个视图。一种视图是 TextView,另一种是 EditText。我想让他们两个都对齐。这是截图和代码。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ccc"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:text="S1"
android:background="#110faa">
</TextView>
<EditText
android:layout_width="80dp"
android:layout_height="20dp"
android:background="#102df2"
android:inputType="numberDecimal"
android:importantForAutofill="no">
</EditText>
</LinearLayout>
这就是现在的样子我不希望 S1 文本视图上有顶部间距。
截图如下:
使用android:layout_weight
分割LinearLayout的宽度和
android:baselineAligned="false"
顶部间距
<LinearLayout android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ccc"
android:orientation="horizontal"
android:layout_weight="5"
android:baselineAligned="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_weight="1"
android:background="#110faa"
android:text="S1">
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_weight="4"
android:background="#102df2"
android:importantForAutofill="no"
android:inputType="numberDecimal"></EditText>
</LinearLayout>
你可以这样做:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ccc"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="S1"
android:background="#110faa">
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:background="#102df2"
android:inputType="numberDecimal"
android:importantForAutofill="no"/>
</LinearLayout>