在中间有间隙的线性布局中创建两列

Creating two columns in a linearlayout with a gap in the middle

如何创建具有两列的布局,一个文本视图在左侧,另一个在右侧,中间有间隙?。我已经在这里 Android: creating two columns in a linearlayout 看到了答案。但是我需要在两列之间有一定的 space 。请有人帮忙。

如果我没理解错的话,您希望两列宽度相同,中间有一个 space。是这样吗:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <!-- First column -->

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My Column 1 Text"/>

    </LinearLayout>

    <!-- Space in-between -->

    <Space
        android:layout_width="25dp"
        android:layout_height="match_parent" />

    <!-- Second column-->

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="My Column 2 Text"/>

    </LinearLayout>

</LinearLayout>

使用您所指的答案,您必须在中间添加一个视图,宽度为 0dp,但权重为 0.1、0.2 等,具体取决于您的差距。

Creating two columns in a linearlayout with a gap in the middle

您可以使用 View 来弥补两个 LinearLayout

之间的差距

试试这个

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="#88FF0000">
        <TextView
            android:layout_width="match_parent"
            android:text="Nilesh"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <View
        android:layout_width="10dp"
        android:background="@color/colorPrimary"
        android:layout_height="match_parent"/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="#88FF0000">

        <TextView
            android:layout_width="match_parent"
            android:text="Nilesh"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

输出

你可以在中间加一个View

<LinearLayout
    android:layout_margin="30dp"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    <View
        android:layout_width="8dp"
        android:background="@android:color/white"
        android:layout_height="match_parent" >
    </View>
    <EditText
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
</LinearLayout>

我会在每列上留一些边距,这样您就可以控制中间的 space。对我来说这是最简单的方法。

<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout android:id="@+id/linearLayout2"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="left"
        xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginEnd="25dp">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Street"
        android:background="#88FF0000"/>
</LinearLayout>

<LinearLayout android:id="@+id/linearLayout3"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginStart="25dp">
    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="456546546"
        android:layout_gravity="right"
        android:background="#8800FF00"/>
</LinearLayout>
</LinearLayout>