在线性布局中创建两列,中间有一个间隙

时间:2018-08-23 08:44:11

标签: android android-layout android-linearlayout

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

5 个答案:

答案 0 :(得分:2)

使用您要指出的答案,您必须在中间添加一个视图,宽度为0dp但权重为0.1、0.2,视您的间隙而定。

答案 1 :(得分:1)

如果我的理解正确,您想拥有两列相同宽度的列,它们之间有一个空格。是这样:

<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>

答案 2 :(得分:1)

  

在线性布局中创建两个列,中间留一个间隙

您可以使用 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>

输出

enter image description here

答案 3 :(得分:1)

您可以在中心添加一个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>

答案 4 :(得分:0)

我会在每列上留一些边距,以便您可以控制它们之间的间隔。对我来说,这是最简单的方法。

<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>
相关问题