1

注意:如果您因为遇到类似问题而来到这里,请确保您阅读并尝试提供的所有答案。似乎在某些情况下,一种可能比另一种更好。所以也做你自己的测试!

前言

我得到了这样的listview的行布局(这是我的真实布局的精简版,所以不要评论包裹在LinearLayout中的单个元素等内容 - 它只是为了说明问题 - 最终版本在那里有更多元素):

在此处输入图像描述

XML 文件在下面供参考,但我简要描述了我想要实现的目标:

  • 蓝色框(图标)和“这里通常是长文本”TextView 是行“真实”内容(item_main_container)。它可以是任何东西——它应该是无关紧要的。此图层内容不可点击。事实上(但我刚刚意识到)我应该从下一层显示“蓝色盒子”一半被红色覆盖,一半被绿色盒子覆盖。
  • 红色和绿色框是我将行的 OnCliclListeners 绑定到(包裹在item_click_area_container)的视图 - 我只希望用户能够点击该行,不管item_main_container它的内容是什么(所以这些红色/绿色在应用程序中是透明的) . 注意:这些在图像上设置为透明,因此您可以item_main_content通过它看到,但这仅用于演示目的。另请注意,红色和绿色代表单独的操作,我需要能够在item_click_area_container图层上有更多元素才能根据用户点击的位置处理更多操作。
  • 黄色区域 ( button_bar_container) 包含用户可能想要点击的按钮。

所以基本上item_click_area_container覆盖item_main_container,但不是button_bar_containeritem_click_area_container否则用户将无法点击其任何按钮。从长远来看,它将是:

在此处输入图像描述

布局大纲如下所示:

在此处输入图像描述


问题

尽管我很努力,但我无法item_click_area_container正确地拉伸以覆盖整体。 item_main_container由于"usually long text here"文本可以是任意长度,我不知道item_main_container. 在下面的 XML 中有android:layout_height="100dp- 这适用于我看到所有行元素,但是对于较长的文本item_click_area_container高度太小。但是,如果我将其替换为,android:layout_height="fill_parent"则整体item_click_area_container将不可见,这违背了目的。


问题

  1. 为什么这不能按预期工作和/或如何解决?
  2. 有没有比使用像我这样的方法item_click_area_container(但没有基于 OnTouchListener 的解决方案)更好的方法来获得内容独立的点击区域?

布局 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="fill_parent" 
          android:layout_height="wrap_content">

        <LinearLayout 
            android:id="@+id/item_main_container"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content">

            <ImageView 
               android:id="@+id/item_avatar" 
               android:layout_width="40dp" 
               android:layout_height="40dp" 
               android:background="#0000ff" 
               android:src="@drawable/icon_help"/>

            <LinearLayout 
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content" 
               android:orientation="vertical">

               <TextView android:id="@+id/item_body" 
                 android:layout_width="fill_parent" 
                 android:layout_height="fill_parent" 
                 android:text="Usually long text here..." 
                 android:textColor="#ffffff"/>
            </LinearLayout>

        </LinearLayout>

        <LinearLayout 
               android:id="@+id/item_click_area_container" 
               android:layout_width="fill_parent" 
               android:layout_height="100dp">
            <LinearLayout 
               android:id="@+id/green_click" 
               android:layout_width="fill_parent" 
               android:layout_height="fill_parent" 
               android:layout_weight="1" 
               android:background="#7700ff00">
            </LinearLayout>
            <LinearLayout 
               android:id="@+id/red_click" 
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:layout_weight="2" 
               android:background="#77ff0000">
            </LinearLayout>
        </LinearLayout>

    </RelativeLayout>

    <LinearLayout 
       android:id="@+id/button_bar_container" 
       android:layout_width="fill_parent" 
       android:layout_height="50dp" 
       android:layout_marginTop="3dp" 
       android:background="#ffff00">
    </LinearLayout>

</LinearLayout>

编辑#1

我想我可以使用OnTouchListener, 并onTouch()检查event.getAction()是否MotionEvent.ACTION_DOWN读取了事件的 X/Y 并检查视图的边界。这可能会奏效,但如果可能的话,我更愿意单独使用 OnClickListener。更不用说它有助于理解当前的罪魁祸首是什么,尤其是在 ADT 中正确呈现布局

4

3 回答 3

3

在这里,您可以使用此布局。简单的解决方案是使用这 4 条线将 的 4 条边item_click_area_container与相应的 4条边对齐:item_main_container

        android:layout_alignTop="@+id/item_main_container"
        android:layout_alignBottom="@+id/item_main_container"
        android:layout_alignLeft="@+id/item_main_container"
        android:layout_alignRight="@+id/item_main_container"

这是带有 wrap_content 高度的布局。没有更多的硬编码大小(:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/item_main_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/item_avatar"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="#0000ff"
                android:src="@drawable/ic_action_search" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/item_body"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="Usually long text hasdkfn ash jsjkgh sjhgjk hasjkh ash fjk hasdjkh fkjash jkdf hjkasdh fjkhasjkg hjkasjkd hfjkash gsdgh kgh sdfh fjksah djkfasd jkg jsdf asgjf sg sd agfgas fjhasg f asdgf gjhsdg sdjhsdg fasdfjhs agjhfg  adf asdg jh dvhsdvjhas gdh fv gsfjivhdfvjksfgjkdhjhixcgzvjk gdshg dfg v gvgdfgvjkshvjksdgvsdvere..."
                    android:textColor="#000" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_alignTop="@+id/item_main_container"
            android:layout_alignBottom="@+id/item_main_container"
            android:layout_alignLeft="@+id/item_main_container"
            android:layout_alignRight="@+id/item_main_container"
            android:id="@+id/item_click_area_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/green_click"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#7700ff00" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/red_click"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="2"
                android:background="#77ff0000" >
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/button_bar_container"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginTop="3dp"
        android:background="#ffff00" >
    </LinearLayout>

</LinearLayout>
于 2012-11-02T19:22:12.747 回答
2

你可以尝试更换你的

<RelativeLayout
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">...</RelativeLayout>

<FrameLayout
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">...</FrameLayout>

然后将参数设置item_click_area_containerfill_parent/match_parent使其增长到覆盖与 相同的区域item_main_container

如果您使用扩展类LinearLayout来处理红色和绿色区域所需的操作,那么您应该没问题。

于 2012-10-31T03:50:50.423 回答
0

你可以做这样的事情。

在 layout.xml android:onClick 中的单个元素上

    <LinearLayout 
       android:id="@+id/red_click" 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="2" 
       android:background="#77ff0000">
   android:onClick="redAreaClicked"
    </LinearLayout>

在您的 java 文件中使用以下代码编写一个方法 redAreaClicked()。

    public void redAreaClicked(View v) {
      RelativeLayout ParentRow = (RelativeLayout) v.getParent();
      final int position = mList.getPositionForView(ParentRow);
      // more code here..
    }

这应该会给你在列表中的位置。即行号。但是要使其正常工作,您必须将所有子视图(彩色视图,在您的情况下为线性布局)移动到单个父级中。使用相对布局我认为这是非常可行的。

否则,您可以使用 view.getId() 并进行比较,以获得父视图。然后确定它在列表中的位置。

您可以对其他颜色区域执行相同操作。

于 2012-10-31T04:16:06.137 回答