簡體   English   中英

如何在兩個線性布局之間放置一條線?

[英]How do i place a line between two linearlayouts?

如何在兩個線性布局之間水平放置一條線? 我嘗試了一個額外的linearlayout,一次嘗試了一個View。 我的linearlayouts當前看起來像這樣:

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        >

        <LinearLayout
            android:id="@+id/about"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:gravity="start"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:background="?android:attr/selectableItemBackground"
            >
            <ImageView
                android:id="@+id/about_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:layout_marginStart="20dp"
                android:layout_gravity="start|center_vertical"
                android:src="@drawable/ic_info_black_24dp"
                android:clickable="false"
                />

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_gravity="start|center_vertical"
                android:text="About"
                android:textSize="20sp"
                android:textColor="@android:color/black"
                android:clickable="false"
                />
        </LinearLayout>
        <!--<View
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:layout_weight="2"
            android:elevation="0dp"
            android:background="@android:color/black"
            />-->

        <LinearLayout
            android:id="@+id/theme"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:layout_weight="3"
            android:gravity="end"
            android:layout_gravity="end"
            android:background="?android:attr/selectableItemBackground"
            >
            <ImageView
                android:id="@+id/theme_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:layout_marginEnd="10dp"
                android:layout_gravity="end|center_vertical"
                android:src="@drawable/ic_palette_white_24dp"
                android:tint="@android:color/black"
                android:clickable="false"
                />
            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end|center_vertical"
                android:layout_marginEnd="20dp"
                android:text="Theme"
                android:textSize="20sp"
                android:textColor="@android:color/black"
                android:clickable="false"
                />
        </LinearLayout>
    </LinearLayout>

當前看起來像這樣(右側的布局比左側多一些,不明白為什么^^): 在此處輸入圖片說明

有什么建議么?

為什么不使用RelativeLayout並避免一起使用權重。 嘗試這個:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">

<View
    android:id="@+id/divider"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:elevation="0dp"
    android:background="@android:color/black" />
<LinearLayout
    android:id="@+id/about"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:layout_toLeftOf="@id/divider"
    android:background="?android:attr/selectableItemBackground">
    <ImageView
        android:id="@+id/about_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:layout_marginStart="20dp"
        android:layout_gravity="start|center_vertical"
        android:src="@drawable/ic_launcher"
        android:clickable="false"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_gravity="start|center_vertical"
        android:text="About"
        android:textSize="20sp"
        android:textColor="@android:color/black"
        android:clickable="false"
        />
</LinearLayout>

<LinearLayout
    android:id="@+id/theme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:layout_toRightOf="@id/divider"
    android:background="?android:attr/selectableItemBackground">
    <ImageView
        android:id="@+id/theme_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:layout_marginEnd="10dp"
        android:layout_gravity="end|center_vertical"
        android:src="@drawable/ic_launcher"
        android:tint="@android:color/black"
        android:clickable="false"
        />
    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|center_vertical"
        android:layout_marginEnd="20dp"
        android:text="Theme"
        android:textSize="20sp"
        android:textColor="@android:color/black"
        android:clickable="false"
        />
</LinearLayout>

擺脫權重,並使用相對布局保存線性布局。 然后將其添加到您的XML中:

<View android:id="@+id/separator"
  android:layout_width="5dp" 
  android:layout_height="match_parent"
  android:layout_centerHorizontal="true"/>

然后在您要右側的布局中添加以下行:

android:layout_toRightOf="@+id/separator"

然后對於左側的布局:

android:layout_toLeftOf="@+id/separator"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM