android自定义控件

最近了解了一下自定控件方面的,刚刚开始接触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("取消");
 }

}

 看看运行效果吧

初始效果

点击后的效果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值