Android 自定义控件简单Demo
来源:互联网 发布:图片生成视频软件 编辑:程序博客网 时间:2024/05/29 04:10
Android 自定义控件简单Demo
本篇所实现的是一个上面是图片,下面是文字的一个按钮,作为最简单的一种自定义控件,用户可以根据自己的需求添加其他组件进行组合,实现更多更实用的效果。
一、自定义控件的xml布局文件
image_btn_layout.xml代码如下:
<?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="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textStyle="bold" android:textColor="#823783" android:text="主页" /></LinearLayout>
二、继承LinearLayout自定义控件
实现比较简单,不再一一解释,ImageBtn.java代码如下:
public class ImageBtn extends LinearLayout { private ImageView imageView; private TextView textView; public ImageBtn(Context context) { super(context, null); } public ImageBtn(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.image_btn_layout, this, true); imageView = (ImageView) findViewById(R.id.image); textView = (TextView) findViewById(R.id.text); } public void setImageResource(int resId) { imageView.setImageResource(resId); } public void setTextViewText(String text) { textView.setText(text); }}
三、在MainActivity的布局文件中加入自定义控件
加入xml中时,采用包名+类名的方式,activity_main.xml布局文件代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.wangkeke.customwidgetdemo.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" > <com.wangkeke.customwidgetdemo.ImageBtn android:id="@+id/image_btn_one" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <com.wangkeke.customwidgetdemo.ImageBtn android:id="@+id/image_btn_two" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <com.wangkeke.customwidgetdemo.ImageBtn android:id="@+id/image_btn_three" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout></RelativeLayout>
四、在MainActivity.java中具体实现控件初始化
public class MainActivity extends Activity implements OnClickListener{ private ImageBtn btn_one; private ImageBtn btn_two; private ImageBtn btn_three; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_one = (ImageBtn) findViewById(R.id.image_btn_one); btn_one.setOnClickListener(this); btn_two = (ImageBtn) findViewById(R.id.image_btn_two); btn_two.setOnClickListener(this); btn_three = (ImageBtn) findViewById(R.id.image_btn_three); btn_three.setOnClickListener(this); init(); } /** * 初始化控件 */ public void init() { btn_one.setImageResource(R.drawable.emoji_177); btn_one.setTextViewText("雪花"); btn_two.setImageResource(R.drawable.emoji_178); btn_two.setTextViewText("祝福"); btn_three.setImageResource(R.drawable.emoji_179); btn_three.setTextViewText("OK"); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.image_btn_one: Toast.makeText(MainActivity.this, "您点击了雪花", Toast.LENGTH_SHORT).show(); break; case R.id.image_btn_two: Toast.makeText(MainActivity.this, "您点击了祝福", Toast.LENGTH_SHORT).show(); break; case R.id.image_btn_three: Toast.makeText(MainActivity.this, "您点击了OK", Toast.LENGTH_SHORT).show(); break; default: break; } }}
五、最后效果图如下:
六、资源下载地址
下载地址:Android 自定义控件Demo下载.
0 0
- Android 自定义控件简单Demo
- android自定义控件(一)之一个简单Demo
- Android 自定义控件demo集合
- android 自定义组合控件[有Demo]
- Android自定义控件移动及demo
- Android简单自定义控件做法
- Android自定义简单控件(一)
- Android自定义简单控件(二)
- Android简单自定义控件做法
- Android自定义控件:老版优酷的三级菜单(效果图 + Demo)
- android UI(2)自定义控件简单使用
- Android自定义组合控件---简单导航栏
- Android自定义组合控件---简单导航栏
- 漂亮简单的Android 自定义 Switch 控件!
- android的自定义控件简单(一)
- android的自定义控件简单(二)
- android自定义控件的简单实现
- Android自定义控件简单实现ratingbar效果
- dispatcherServlet
- c++函数名前面&的问题
- LeetCode: Longest Substring Without Repeating Characters
- Closest Sums - UVa10487 二分
- C++函数模板和类模板
- Android 自定义控件简单Demo
- hdu 4267 A Simple Problem with Integers 线段树维护不连续点
- 堆栈的区别
- C++指向类成员的指针的使用(详细介绍)
- C++ 类的静态成员详细讲解(一)
- C++ 类的静态成员详细讲解(二)
- 重载运算符
- char *a="hello";
- strcpy函数