【问题标题】:Creating two columns in a linearlayout with a gap in the middle在中间有间隙的线性布局中创建两列
【发布时间】:2018-08-23 08:44:11
【问题描述】:

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

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

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

    【讨论】:

    • 是的,这也是一种方法。虽然为此有一个专用的Space 标签:)
    • 你的建议没问题。我认为,正如@georgy-savatkov 所说,专用的 Space 就足够了。
    【解决方案2】:

    如果我理解正确,您希望有两列宽度相同,中间有一个空格。是这样的:

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

    【讨论】:

    • 正是我需要的。感谢您的快速帮助。
    【解决方案3】:

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

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

    输出

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        我会在每一列上设置一些边距,以便您控制其间的空间。对我来说是最简单的方法。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-13
          • 2019-09-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多