最近了解了一下自定控件方面的,刚刚开始接触android,随便写写
首先需要在一个xml文件中根据需要配置
<?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:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="12dip" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="4dip" />
</LinearLayout>
上面就是我定义的一个xml,一个ImageView和一个TextView
下面要做的就是在一个class继承LinearLayout实例化上面的布局
package cn.xxs.zidingyi2;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.xxs.zidingyi.R;
/**
* 此类是用于实例化自定义控件的
* @author xxs
*
*/
public class LinearButton extends LinearLayout {
private ImageView image;
private TextView text;
/**
* @param context 上下文,可以理解为当前类
* @param attrs 属性设置,View
*/
public LinearButton(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// 通过SystemService获得layout扩展服务
inflater.inflate(R.layout.zidingyibutton, this);// 实例化xml布局文件
image = (ImageView)findViewById(R.id.img1);//拿到相应控件
text = (TextView)findViewById(R.id.text1);
}
/**
* 设置显示的图片
* @param resId 图片ID
*/
public void SetImage(int resId){
image.setImageResource(resId);
}
/**
* 显示的文字
* @param txt 文字内容
*/
public void SetText(String txt){
text.setText(txt);
}
}
接下将定义好的控件加入需要的xml文件里面
<?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" >
<cn.xxs.zidingyi2.LinearButton
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button2"
android:clickable="true" />
<cn.xxs.zidingyi2.LinearButton
android:id="@+id/error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button2"
android:clickable="true" />
</LinearLayout>
在上面的xml中设置了图片点击效果(android:background="@drawable/button2"
),只需在drawable中写入相应的xml即可(将图片换成自己的图片就是啦)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bt3" android:state_focused="true" android:state_pressed="false"></item>
<item android:drawable="@drawable/bt4" android:state_pressed="true"></item><!-- 点击显示 -->
<item android:drawable="@drawable/bt4" android:state_checked="true"></item><!-- 选中显示 -->
<item android:drawable="@drawable/bt3" android:state_focused="false" android:state_pressed="false"></item>
</selector>
现在就在一个Activity中填充我们定义好的xml布局文件,并设置自己的内容就可以了
package cn.xxs.zidingyi2;
import cn.xxs.zidingyi.R;
import android.app.Activity;
import android.os.Bundle;
/**
* 加载自定义的控件显示
* @author xxs
*/
public class LinearActivity extends Activity {
private LinearButton bt1;//基于自定义控件实例化类定义两个控件
private LinearButton bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zidingyi);//填充layout布局文件
bt1 = (LinearButton)findViewById(R.id.right);//拿到layout文件先定义的两个孔家
bt2 = (LinearButton)findViewById(R.id.error);
bt1.SetImage(R.drawable.a066);//为控件添加图片显示
bt2.SetImage(R.drawable.a067);
bt1.SetText("确定");//为控件添加文字显示
bt2.SetText("取消");
}
}
看看运行效果吧
初始效果
点击后的效果