android - 如何在每一行中有多个 TextView 和 EditText

标签 android android-layout android-edittext textview

我试图获得这样一种形式,即在每一行上 TextView 位于左侧,EditText 位于右侧。

下面的代码适用于一行,但是,如果我有多行,那么它不会在每一行上绘制 TextView 和 EditText,而是试图将所有内容塞在一起。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dip"
    android:background="#FFFFFF"
    >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Full Name"
        android:textSize="20dp"
        android:layout_weight="1"
        android:textColor="#000000"
 />

    <EditText
        android:id="@+id/name"
        android:gravity="center"
        android:hint="John Doe"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Age"
        android:textSize="20dp"
        android:layout_weight="1"
        android:textColor="#000000"
        />

    <EditText
        android:id="@+id/age"
        android:gravity="center"
        android:hint="age"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />

</LinearLayout>

这是它的样子

enter image description here

最佳答案

有很多方法可以实现您一直在寻找的 Layout。您通过在 View 中添加 layout_weight="1" 步入正轨。这是使用您的 Layout 且仅对 orientation 进行少量更改的布局。

解释:

LinearLayout  //ParentView Orientation - Vertical
     LinearLayout //childView Orientation - Horizontal
        TextView & EditText // Taking equal space with help of layout_weight="1"
     LinearLayout //childView Orientation - Horizontal
        TextView & EditText // Taking equal space with help of layout_weight="1"

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:background="#FFFFFF"
>
    <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     >
       <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Full Name"
        android:textSize="20dp"
        android:layout_weight="1"
        android:textColor="#000000"
        />

       <EditText
        android:id="@+id/name"
        android:gravity="center"
        android:hint="John Doe"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
       />
    </LinearLayout>
    <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     >
     <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Age"
      android:textSize="20dp"
      android:layout_weight="1"
      android:textColor="#000000"
      />
     <EditText
      android:id="@+id/age"
      android:gravity="center"
      android:hint="age"
      android:layout_width="fill_parent"
      android:layout_weight="1"
      android:layout_height="wrap_content"
      />
    </LinearLayout> 
</LinearLayout>

关于android - 如何在每一行中有多个 TextView 和 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23583051/

相关文章:

android - 软键盘方向键

android - 如何设计一个两列抽屉导航

java - 如何通过 Android 智能手机与汽车通信?

android - ListView 内的可聚焦 EditText

android-layout - com.android.internal.R.id.content 无法解析为闹钟代码中的变量

Android - EditText 作为搜索框不起作用

android - 是否可以将 TableLayout 与 ArrayAdapter 绑定(bind)?

android - 仅在开发中启用严格模式

android - 具有渐变填充和曲线形状的 Canvas 绘图

android - EditText setError 在 PopupWindow 中不起作用