Android如何实现自定义控件
来源:互联网 发布:如何仿真网络性能测试 编辑:程序博客网 时间:2024/04/27 17:46
学习Android也有一个月的时间了,一直不知道怎么能够实现自定义的 控件!今天学习了下,做了一个简单的demo!
我想要实现如下的效果:
首先:我们要定义要按钮内部的布局结构 custom_btn.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:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingLeft="10.0dip" android:paddingTop="10.0dip" android:paddingBottom="10.0dip" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_marginLeft="8dip" android:layout_gravity="center_vertical" android:paddingLeft="5.0dip" android:paddingTop="10.0dip" android:paddingBottom="10.0dip" android:paddingRight="10.0dip" android:textSize="18.0sp" /> </LinearLayout>
其次:我们需要写一个类来集成LinearLayout,导入刚刚定义的按钮布局,从而使的能在代码中控制这个自定义控件内容的显示。代码如下:
CustomButton.java
package com.example.zuheui;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 CustomButton extends LinearLayout {private ImageView iv;private TextView tv;public CustomButton(Context context, AttributeSet attrs) {super(context, attrs);//导入布局LayoutInflater.from(context).inflate(R.layout.custom_btn, this, true);iv=(ImageView) findViewById(R.id.iv);tv=(TextView) findViewById(R.id.tv);}/* * 设置图片资源 */public void setImageResource(int resId){iv.setImageResource(resId);}/* * 设置显示文字 */void setTextViewText(String text) {// TODO Auto-generated method stubtv.setText(text);}}
再次,在需要使用这个自定义控件的layout中加入这控件,只需要在activity_main.xml中加入即可。方法如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.example.zuheui.CustomButton android:id="@+id/bt_confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_bg" /> <com.example.zuheui.CustomButton android:id="@+id/bt_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_bg" /> </LinearLayout>最后一步,即在activity中设置该控件的内容
MainActivity.java
package com.example.zuheui;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Toast;public class MainActivity extends Activity {private CustomButton bt_confirm;private CustomButton btn_cancel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt_confirm=(CustomButton) findViewById(R.id.bt_confirm);btn_cancel=(CustomButton) findViewById(R.id.bt_cancel);bt_confirm.setTextViewText("确定");bt_confirm.setImageResource(R.drawable.ic_launcher);btn_cancel.setTextViewText("取消");btn_cancel.setImageResource(R.drawable.ic_launcher);bt_confirm.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();}});btn_cancel.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
@author www.lelexie.com
本文出自:恋人SEO---http://www.loverseo.com
- Android如何实现自定义控件
- android自定义控件实现
- Android自定义控件实现
- Android自定义控件实现
- Android如何使用自定义控件
- android 如何打包自定义控件
- android 如何打包自定义控件
- Android.自定义控件的实现
- Android.自定义控件的实现
- Android自定义控件的实现
- Android自定义控件的实现
- Android 自定义控件 实现 创建
- Android.自定义控件的实现
- Android 自定义控件实现分析
- Android自定义控件实现时钟
- Android自定义控件的实现
- Android 自定义实现日历控件
- .NET自定义控件应该如何实现?
- 【Deep Learning学习笔记】Dynamic Auto-Encoders for Semantic Indexing_Mirowski_NIPS2010
- 安装efence库
- [JavaScript]截断字符串,解决中文1个字只算了1个字符的问题
- C++中引用和指针的区别
- 几篇关于javascript的好文章,收藏
- Android如何实现自定义控件
- lmctfy:Google的开源Linux容器
- TCP协议
- 在cmd命令行下编译运行C/C++源文件
- C++构造函数学习笔记
- 回调函数
- Java Web学习笔记——概论
- #Github#个人常用命令
- LIBGDX 源码分析 OrthographicCamera glViewport