簡體   English   中英

LinearLayout在底部有空白

[英]LinearLayout with blank space at the bottom

我正在使用LinearLayout水平從適配器加載視圖。 盡管我使用wrap_content,並且布局中的視圖始終對齊到頂部。 在linearLayout下面有一個空格。 這是錯誤還是我做錯了。 請檢查附件 截圖

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:clickable="true"
        android:background="@drawable/recycler_selector">

        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:id="@+id/group_chat_photo" />

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_weight="5"
            android:paddingStart="5dp"
            android:paddingEnd="5dp"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"
                android:layout_height="wrap_content"
                style="@style/ListTitleTextStyle"
                android:id="@+id/group_name" />

            <TextView
                android:layout_width="wrap_content"
                android:padding="5dp"
                android:layout_toStartOf="@id/group_name"
                android:layout_height="wrap_content"
                android:id="@+id/num_of_group_users"
                style="@style/PresenceTextStyle"
                android:layout_toEndOf="@id/group_name"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/SubTitleTextStyle"
                android:layout_below="@id/group_name"
                android:id="@+id/group_members_presence"/>

        </RelativeLayout>

        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_dots_vertical_grey600_24dp"
            android:contentDescription="@string/group_chat_options"
            android:id="@+id/group_chat_options"/>


    </LinearLayout>

這個元素是主要問題,

<TextView
  android:id="@+id/num_of_group_users"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toEndOf="@id/group_name"
  android:layout_toStartOf="@id/group_name"
  tools:text="10 users"
  android:padding="5dp" />

由於您已經在使用相對布局,因此無需將其包裝在線性布局周圍。 您可以將單個RelativeLayout用作容器。 例如,以下只是一個想法,您可以根據需要更改填充和邊距。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@android:color/holo_orange_dark"
  android:orientation="horizontal">

  <ImageView
    android:id="@+id/group_chat_photo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@mipmap/ic_launcher" />

  <TextView
    android:id="@+id/group_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/group_chat_photo"
    tools:text="Some title" />

  <TextView
    android:id="@+id/num_of_group_users"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/group_name"
    android:layout_toRightOf="@+id/group_members_presence"
    tools:text="10 users" />

  <TextView
    android:id="@+id/group_members_presence"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/group_name"
    android:layout_toRightOf="@+id/group_chat_photo"
    tools:text="Presence" />

  <ImageView
    android:id="@+id/group_chat_options"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@mipmap/ic_launcher" />

</RelativeLayout>

嘗試將整個線性布局放入另一個布局(相對或線性)中,並將其寬度設置為“ match_parent”,將高度設置為“ wrap_content”

我認為問題在於您在其中一個TextView中使用layout_toStartOf和layout_toEndOf(android:id =“ @ + id / num_of_group_users”)刪除其中之一。

不要對RelativeLayout使用LinearLayout屬性android:layout_weight 我已經使用RelativeLayout重新設計了您的xml。 有關輸出,請參見所附圖像。

這是工作代碼。 嘗試這個:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="82dp"
    android:clickable="true"
    android:background="#455A64"
    android:padding="16dp">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/group_chat_photo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:src="@mipmap/ic_launcher"/>

    <ImageView
        android:id="@+id/group_chat_options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_dots_vertical_grey600_24dp"
        android:contentDescription="group_chat_options" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/group_chat_photo"
        android:layout_toLeftOf="@id/group_chat_options"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp">

        <TextView
            android:id="@+id/group_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Americal AllStars"
            android:textColor="#FFFFFF"
            android:textSize="18dp"/>

        <TextView
            android:id="@+id/group_members_presence"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/group_name"
            android:text="Alucard, Client"
            android:textColor="#0788C9"
            android:textSize="16dp" />

        <TextView
            android:id="@+id/num_of_group_users"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/group_members_presence"
            android:layout_alignBottom="@id/group_members_presence"
            android:paddingLeft="5dp"
            android:text="and 10 others"
            android:textColor="#0788C9"
            android:textSize="16dp" />

    </RelativeLayout>
</RelativeLayout>

OUTPUT:

在此處輸入圖片說明

希望對您有幫助〜

暫無
暫無

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

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