关于动态生成安卓界面控件并传入点击事件

本文介绍如何在Android应用中动态创建界面控件,并为这些控件设置点击事件。通过自定义方法fillView,从数据库获取数据并填充到布局中,每个控件设置tag以便区分点击。读者可以通过提供的布局文件zitaiomu.xml和MainActivity.java了解实现细节。
摘要由CSDN通过智能技术生成

动态生成界面其实不难,主要方法写在fill view中,这是一个自定义的方法,参数大家可以自行修改,这里我用到的是传了一个数据库中的list 对象。为了判定是哪个图片点击,设置了一个tag 属性,代码简答自行理解,不懂的可以留言。

每一小项的布局文件:zitaiomu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
     android:layout_weight="1.0"
    android:clickable="true" >
 
    <ImageView
        android:id="@+id/image_icon"
        android:layout_width="170dp"
        android:layout_height="112dp"
        android:layout_marginTop="3dp"
         />

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" >

    <TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="jjjj"
        android:textSize="19dp" 
        android:textStyle="normal"/>
     <ImageView
         android:layout_marginTop="5dp"
        android:id="@+id/xiangqing"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:src="@drawable/detail_again" />
 </LinearLayout>
</LinearLayout>

主文件 MainActivity.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值