我是 Android 应用程序开发的新手,虽然到目前为止我对编码方面的理解还不错,但我无法全神贯注于布局。我正在尝试创建一个应用程序,其中 EditText
元素的大小均匀,尽管它们的 TextView
对应元素的大小。应用现在的样子在左边,我想要的在右边。
这是我的布局代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".RandApp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical"
android:id="@+id/gridLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="0"
android:layout_column="0"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/one"
android:id="@+id/lblOne"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtOne"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:textAlignment="center" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="1"
android:layout_column="0"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/two"
android:id="@+id/lblTwo"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtTwo"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:textAlignment="center" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="1"
android:layout_column="0"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/three"
android:id="@+id/lblThree"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtThree"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:textAlignment="center" />
</TableRow>
</LinearLayout>
</RelativeLayout>
我该怎么做才能达到预期的效果?布局代码主要是使用 Android Studio 的设计器创建的,我只做了几处小改动。
最佳答案
尝试使用重量..
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical"
android:id="@+id/gridLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="0"
android:layout_column="0"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="one"
android:id="@+id/lblOne"
android:layout_gravity="center_vertical"
android:layout_weight="2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtOne"
android:layout_gravity="center_vertical"
android:layout_weight="4"
android:textAlignment="center" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="1"
android:layout_column="0"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="two"
android:id="@+id/lblTwo"
android:layout_gravity="center_vertical"
android:layout_weight="2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtTwo"
android:layout_gravity="center_vertical"
android:layout_weight="4"
android:textAlignment="center" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="1"
android:layout_column="0"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="three"
android:id="@+id/lblThree"
android:layout_gravity="center_vertical"
android:layout_weight="2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtThree"
android:layout_gravity="center_vertical"
android:layout_weight="4"
android:textAlignment="center" />
</TableRow>
</LinearLayout>
</RelativeLayout>
关于Android 布局 - 使 EditText 行大小均匀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33578163/