自定义android控件
来源:互联网 发布:基于python的paas 编辑:程序博客网 时间:2024/06/11 06:05
新建一个Android工程
保证下面的文件布局,标上了*号的是需要自己加上去的,代码马上贴出来
+userControl
+src
+blacklaw.product.usercontrol
-ImageBtn.java(自定义控件的类)*1-1
-MainActivity.java
+res
+drawable
-btn.xml(自定义控件的背景布局,可选)*2-1
+layout
-activity_main.xml
-imagebtn.xml(自定义控件的布局)*1-2
1-1自定义控件的类
package blacklaw.product.usercontrol;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 ImageBtn extends LinearLayout { private ImageView imageView; private TextView textView; public ImageBtn(Context context) { super(context); // TODO Auto-generated constructor stub } public ImageBtn(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);inflater.inflate(R.layout.imagebtn, this);imageView=(ImageView) findViewById(R.id.imageView1);textView=(TextView)findViewById(R.id.textView1);} /*** 设置图片资源*/ public void setImageResource(int resId) { imageView.setImageResource(resId); } /*** 设置显示的文字 */ public void setTextViewText(String text) { textView.setText(text); } }1-2
自定义控件的布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:paddingBottom="5dip"android:paddingLeft="40dip"android:paddingTop="5dip"android:src="@drawable/ic_launcher" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginLeft="8dip"android:text="确定"android:textColor="#000000" /></LinearLayout>
控件完成了,两步就行,一个是执行类,一个是布局的xml文件
接着讲一下控件的使用
在activity_main.xml的xml源中直接插入代码
<blacklaw.product.usercontrol.ImageBtn android:id="@+id/btn_error" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp"/>
如果需要背景图片的话就需要增加语句
变成
<blacklaw.product.usercontrol.ImageBtn android:id="@+id/btn_error" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp"android:background="@drawable/btn"/>
背景布局文件btn.xml如下
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/ic_launcher"></item> <item android:state_pressed="true" android:drawable="@drawable/ic_launcher"></item> <item android:state_checked="true" android:drawable="@drawable/ic_launcher"></item> <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/ic_launcher"></item></selector>
最后在mainActivity.class调用就行
public class mainActivity extends Activity { private ImageBtn imageBtn1; private ImageBtn imageBtn2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.identifybutton); imageBtn1=(ImageBtn) this.findViewById(R.id.btn_right); imageBtn1.setTextViewText("确定"); imageBtn1.setImageResource(R.drawable.right_icon);imageBtn1.setOnClickListener(new View.OnClickListener() {public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "点击的正确按钮", 1).show(); } });}}
- [Android自定义控件] Android自定义控件
- Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- Android 自定义控件 单页翻书控件
- android虚线控件---自定义控件
- android自定义控件实例 --控件
- Android自定义控件--组合控件
- android 控件 自定义组合控件
- 【android自定义控件】ProgressBar自定义
- Android自定义控件 自定义属性
- Android自定义控件 -- 自定义View
- Android自定义控件 -- 自定义ViewGroup
- Android 自定义控件 自定义标题栏
- Android 自定义控件-自定义进度条。
- PDFCreator——ps(postscript)转pdf
- ggplot2(三) 语法 part2
- 一个二维数组以顺时针螺旋的方式打印出来
- Matlab中一些常用的东东
- 请教VC6.0如何实现将excel的数据读取出来然后在CListctrl列表上显示
- 自定义android控件
- 完全二叉树的判断
- Predator:比微软Kinect更强的视频追踪算法——来自捷克博士论文
- 安卓电源管理驱动
- java寻找类
- 在右键菜单中添加Dos快捷通道-dos在这里
- TuneUp.Utilities.2013.v13.0.2020.14-TE
- LeetCode: Minimum Path Sum
- SQL SEVER 报错;未删除任何行,已更新或删除的行值要么不能是该行成为唯一行,要么改变了多个行。