zoukankan      html  css  js  c++  java
  • ListView控件的列表项的文字不满一行的时候,如何实现点击该列表项的空白区域仍可触发列表项的点击事件

    今天在做Demo的过程中,使用到了ListView。然而在实现过程中,发现一个出现了一个问题:只能点击列表项的文字区域可以触发点击事件,而点击列表项的空白区域无法触发点击事件。

    如下图:

    listitem的布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        
        <TextView
            android:id="@+id/listitemText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             android:text="@string/app_name"
             android:textSize="18sp"
            android:textColor="#000000"
            android:gravity="center_vertical"
            android:padding="15dp"
        />
    
    </LinearLayout>
    listitem

    刚开始以为是这个布局文件中的android:layout_width的问题,后来发现是ListView控件的android:layout_width的问题。

    上图对应的ListView控件的android:layout_width的值为wrap_content。

    <!-- 历史记录列表 -->
                <!-- android:layout_width="match_parent"使用这个则代表列表项占据屏幕宽度 -->
                <!-- android:layout_width="wrap_content"使用这个则代表列表项占据实际宽度 -->
                <ListView
                    android:id="@+id/history_list"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
                </ListView>

    如果想要实现列表项在不满一行的情况下,点击空白区域仍触发列表项的点击事件的话,ListView的写法如下:

    <!-- 历史记录列表 -->
                <!-- android:layout_width="match_parent"使用这个则代表列表项占据屏幕宽度 -->
                <!-- android:layout_width="wrap_content"使用这个则代表列表项占据实际宽度 -->
                <ListView
                    android:id="@+id/history_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </ListView>

    效果图如下:

  • 相关阅读:
    游戏架构草稿(1)
    蔡学镛:架构师最重视的文档
    常见拉丁字母
    图像识别学习1
    .net framework 2.0,3.0与3.5之间的关系 [转载]
    ASP.NET Session丢失问题原因及解决方案[转载]
    PLSQL 循环游标 cursor loop fetch into【转载】
    oracle case when的用法 【转载】
    Oracle to_char格式化函数 [转载]
    oracle表关联应用 【转载】
  • 原文地址:https://www.cnblogs.com/whycxb/p/4858935.html
Copyright © 2011-2022 走看看