自定义控件
请看例子:
定义一个/res/layout/control.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv" android:src="@drawable/icon" android:paddingTop="5dip" android:paddingBottom="5dip" android:paddingLeft="5dip" android:layout_gravity="center_vertical" /> <TextView android:layout_height="wrap_content" android:text="确定" android:textColor="#000000" android:id="@+id/tv" android:layout_marginLeft="8dip" android:layout_marginRight="10dip" android:layout_gravity="center_vertical" android:layout_width="wrap_content"/> </LinearLayout>
自定义控件的类ImageBt.java
package com.notice.ib; import com.control.R; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class ImageBt extends LinearLayout { private ImageView iv; private TextView tv; public ImageBt(Context context) { this(context, null); } public ImageBt(Context context, AttributeSet attrs) { super(context, attrs); // 导入布局 LayoutInflater.from(context).inflate(R.layout.control, this, true); iv = (ImageView) findViewById(R.id.iv); tv = (TextView) findViewById(R.id.tv); } /** * 设置图片资源 */ public void setImageResource(int resId) { iv.setImageResource(resId); } /** * 设置显示的文字 */ public void setTextViewText(String text) { tv.setText(text); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" > <com.notice.ib.ImageBt android:id="@+id/bt_confirm" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentBottom="true" android:background="#FFFFFF" android:clickable="true" android:focusable="true" /> <com.notice.ib.ImageBt android:id="@+id/bt_cancel" android:layout_toRightOf="@id/bt_confirm" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentBottom="true" android:background="#FFFFFF" android:clickable="true" android:focusable="true" android:layout_marginLeft="10px" /> </RelativeLayout> </LinearLayout>
主Activity类
package com.control; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import com.notice.ib.ImageBt; public class ControlActivity extends Activity { private ImageBt ib1; private ImageBt ib2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ib1 = (ImageBt) findViewById(R.id.bt_confirm); ib2 = (ImageBt) findViewById(R.id.bt_cancel); ib1.setTextViewText("确定"); ib1.setImageResource(R.drawable.icon); ib2.setTextViewText("取消"); ib2.setImageResource(R.drawable.icon); ib1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //在这里可以实现点击事件 } }); } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28