android 自定义控件linearlayout,android自定义控件实例(Linearlayout组合TextView和ImageView).docx...

本文介绍了如何在Android中创建一个自定义控件,该控件由LinearLayout组成,包含一个ImageView和一个TextView。通过自定义布局文件和扩展LinearLayout,实现了对控件内容的控制,提供了一种在代码中操作自定义组件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8

8android:id="@+id/imageView1"

8

8android:id="@+id/imageView1"

android 自定义控件实例 (Linearlayout 组合 TextView 和

ImageView)

2013-12-18 11:25:22

转载自:

/lib/view/open1328836804515.

html

很多时候 android 常用的控件不能满足我们的需求,那么我 们就需要自定义一个控件了。今天做了一个自定义控件的实 例,来分享下。

首先定义一个 layout 实现按钮内部布局:

xmlns:android=" /apk/res/andro

id"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

6

7

}

}

9

9android:layout_width="wrap_content"

10android:layout_height="wrap_content"11android:layout_gravity="center_vertical"12android:paddingBottom="5dip"13android:paddingLeft="40dip"14android:paddingTop="5dip"15android:src="@drawable/right_icon" />1617

10

android:layout_height="wrap_content"

11

android:layout_gravity="center_vertical"

12

android:paddingBottom="5dip"

13

android:paddingLeft="40dip"

14

android:paddingTop="5dip"

15

android:src="@drawable/right_icon" />

16

17

18

android:id="@+id/textView1"

19

android:layout_width="wrap_content"

20

android:layout_height="wrap_content"

21

android:layout_gravity="center_vertical"

22

android:layout_marginLeft="8dip"

23

android:text=" 确定 "

24android:textColor="#000000" />

24

25 26

接下来写一个类继承 LinearLayout ,导入刚刚的布局,并且 设置需要的方法,从而使的能在代码中控制这个自定义控件 内容的显示。

1 public class ImageBtn extends LinearLayout {

private ImageView imageView;

private TextView textView;

public ImageBtn(Context context) {

super(context);

10// TODO Auto-generated constructor stub

10

public ImageBtn(Context context, AttributeSet

attrs) { findViewById(R.id.imageView1);

11super(context, attrs);12// TODO Auto-generated constructor stub13LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SER

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值