在列表

时间:2016-09-03 11:51:18

标签: android android-layout

我正在尝试将溢出图标(显示弹出菜单)添加到列表的每一行。每行左侧有图像(70dp * 70dp),右侧有溢出图标图像(27dp * 70dp)。

我目前的布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:paddingLeft="7dp"
    android:paddingStart="7dp"
    android:paddingRight="7dp"
    android:paddingEnd="7dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">

    <!--  ListRow Left side Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginRight="5dip"
        android:layout_marginEnd="5dip">

        <ImageView
            android:id="@+id/neighborlisticon"
            android:contentDescription="@string/icon_description"
            android:layout_width="70dip"
            android:layout_height="70dip"/>
    </LinearLayout>

    <TextView
        android:id="@+id/name"
        android:textSize="16sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/name"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:layout_toEndOf="@+id/thumbnail"/>

    <TextView
        android:id="@+id/middleline"
        android:textSize="13sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/middleline"
        android:layout_below="@id/name"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:layout_toEndOf="@+id/thumbnail"/>

    <TextView
        android:id="@+id/text"
        android:textSize="14sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:layout_below="@id/middleline"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:layout_toEndOf="@+id/thumbnail"/>

    <ImageView
        android:id="@+id/item_overflow"
        android:layout_width="27dp"
        android:layout_height="70dip"
        android:layout_gravity="top|right"
        android:padding="5dp"
        android:scaleType="center"
        android:src="@drawable/ic_more_vert_black_24dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

但有了这个,中间行的文字右侧是溢出图标item_overflow。我不想要的。问题是,点击溢出图标被视为点击行,因此弹出菜单永远不会出现。

如何在RelativeLayout中指定将左侧图像与左侧和右侧图像对齐并使用中间的所有可用空间进行文本视图?

3 个答案:

答案 0 :(得分:2)

您可以在右侧使用此布局重叠图像(三个点)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="7dp"
android:paddingStart="7dp"
android:paddingRight="7dp"
android:paddingEnd="7dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">

<!--  ListRow Left side Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginRight="5dip"
    android:layout_marginEnd="5dip">

    <ImageView
        android:id="@+id/neighborlisticon"
        android:contentDescription="@string/icon_description"
        android:layout_width="70dip"
        android:layout_height="70dip"/>
</LinearLayout>

<TextView
    android:id="@+id/name"
    android:textSize="16sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/name"
    android:layout_toLeftOf="@id/item_overflow"
    android:textStyle="bold"
    android:layout_alignParentTop="true"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:layout_toEndOf="@+id/thumbnail"/>

<TextView
    android:id="@+id/middleline"
    android:textSize="13sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/middleline"
    android:layout_toLeftOf="@id/item_overflow"
    android:layout_below="@id/name"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail"
    android:layout_toEndOf="@+id/thumbnail"/>

<TextView
    android:id="@+id/text"
    android:textSize="14sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:layout_toLeftOf="@id/item_overflow"
    android:layout_below="@id/middleline"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail"
    android:layout_toEndOf="@+id/thumbnail"/>

<ImageView
    android:id="@+id/item_overflow"
    android:layout_width="27dp"
    android:layout_height="70dip"
    android:layout_gravity="top|right"
    android:padding="5dp"
    android:scaleType="center"
    android:src="@drawable/ic_more_vert_black_24dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

机器人:layout_toLeftOf =&#34; @ ID / item_overflow&#34;和android:layout_toStartOf =&#34; @ id / item_overflow&#34;用于文件中的重叠问题。

答案 1 :(得分:1)

  

仅在android:layout_toLeftOf="@+id/item_overflow"下方提供Text Views属性。

<TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toEndOf="@+id/thumbnail"
        android:layout_toLeftOf="@+id/item_overflow"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Testingggggggggg"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/middleline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_marginTop="1dip"
        android:layout_toEndOf="@+id/thumbnail"
        android:layout_toLeftOf="@+id/item_overflow"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Testinggggggg"
        android:textColor="#000000"
        android:textSize="13sp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/middleline"
        android:layout_marginTop="1dip"
        android:layout_toEndOf="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:layout_toLeftOf="@+id/item_overflow"
        android:textColor="#000000"
        android:text="Testing"
        android:textSize="14sp" />

答案 2 :(得分:1)

使用以下代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingEnd="7dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingStart="7dp"
android:paddingTop="5dp">
<!--  ListRow Left side Thumbnail image -->
<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginEnd="5dip"
    android:layout_marginRight="5dip">
    <ImageView
        android:id="@+id/neighborlisticon"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:contentDescription="hello"
        android:src="@drawable/ic_keyboard_backspace_black_24dp" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/item_overflow"
    android:layout_toRightOf="@+id/thumbnail"
    android:orientation="vertical">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignTop="@+id/thumbnail"

        android:layout_toEndOf="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="hellowqeqweqwqwe111111111111111111111111111111111111sadjhfasdjhfhjasdhdfjkqewasDASD"
        android:textColor="#000"
        android:textSize="16sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/middleline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_marginTop="1dip"
        android:layout_toEndOf="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="hellowqeqweqwqweqewgdfsssdjksafsakdfhsadjhkfhjsadkfkjasfaksfkhkassssssssssssssssssssssssssssssssssssssssss"
        android:textColor="#000"
        android:textSize="13sp" />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/middleline"
        android:layout_marginTop="1dip"
        android:layout_toEndOf="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="helloeqwweeeeeeeeeeeeewqeeeewqewqewqeeqeqwewqeeeeeeeeeeeeeeeeasjhdfashfdhashfkalsfklasl"
        android:textColor="#000"
        android:textSize="14sp" />
</LinearLayout>
<ImageView
    android:id="@+id/item_overflow"
    android:layout_width="27dp"
    android:layout_height="70dip"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"

    android:layout_alignParentTop="true"
    android:layout_gravity="top|right"
    android:padding="5dp"
    android:scaleType="center"
    android:src="@drawable/ic_arrow_back_black_24dp" />
</RelativeLayout>

enter image description here