自定义控件(二)

定义自定义控件时所用到的资源文件attrs此处不再示例怎么定义,若要查看请看《自定义控件(一)》

此自定义控件使用性也较高,可以被其他activity很容易的反复调用,下面来看一下实现代码:

调用时的xml代码:

<span style="font-size:14px;">xmlns:myapp="http://schemas.android.com/apk/res-auto"</span>

<span style="font-size:14px;"><costomview.MyOwnView
        android:id="@+id/my_title_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        myapp:text_first="左一"
        myapp:text_two="中间"
        myapp:text_three="右一"

        /></span>
<span style="font-size:14px;">
</span>
<pre name="code" class="java"><span style="font-size:14px;">
</span>
<span style="font-size:14px;">/*
* 自定义控件实现及监听
* */
public class MyOwnView extends RelativeLayout implements View.OnClickListener{
    private LayoutInflater mInflater;
    private OnClickListener mClickListener;
    private String firstTxt,twoTxt,threeTxt;
    public void setMyViewOnClickListener(OnClickListener l){   //activity中注册所调用的方法,接口采用系统定义接口(也可自定义一个接口)
        mClickListener = l;
    }

    public MyOwnView(Context context) {
        super(context);
    }

    public MyOwnView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public MyOwnView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

   public void init(Context context, AttributeSet attrs){
       mInflater = LayoutInflater.from(context);
       View v = mInflater.inflate(R.layout.own_view_layout,this,true);    //联系上下文解析原始布局
       TypedArray a = null;
       try {
           a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.MyOwnView,0,0);
           //获取新布局中引用自定义控件设置的值
           firstTxt = a.getString(R.styleable.MyOwnView_text_first);
          twoTxt = a.getString(R.styleable.MyOwnView_text_two);
          threeTxt = a.getString(R.styleable.MyOwnView_text_three);
       }finally {
           a.recycle();   //回收
       }


       if (firstTxt != null){
           TextView first = (TextView) v.findViewById(R.id.own_first_txt);   //设置值在原始布局中
           first.setText(firstTxt);
           first.setOnClickListener(this);     //此处采用系统接口注册
       }
       if (twoTxt != null){
           TextView two = (TextView) v.findViewById(R.id.own_two_txt);
           two.setText(twoTxt);

       }

       if (threeTxt != null){
           TextView three = (TextView) v.findViewById(R.id.own_three_txt);
           three.setText(threeTxt);
           three.setOnClickListener(this);
       }
   }

    @Override
    public void onClick(View v) {
        if (mClickListener != null){
            mClickListener.onClick(v);
        }
    }
}</span>
<span style="font-size:14px;">activity中实现:</span>
<pre name="code" class="html"><span style="font-size:14px;">public class MyViewTestActivity extends Activity {
    private MyOwnView myOwnView ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myview_layout);

        myOwnView = (MyOwnView) findViewById(R.id.my_title_view);

        myOwnView.setMyViewOnClickListener(new View.OnClickListener() {   //实现监听
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.own_first_txt:
                        finish();
                        break;
                    case R.id.own_three_txt:
                        Toast.makeText(MyViewTestActivity.this,"点击成功",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }
}</span>

 
 

设置所用到的原始布局,

<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@android:color/holo_blue_light">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/own_first_txt"
        android:textSize="14sp"
        android:layout_marginLeft="10dp"
        android:padding="5dp"
        android:layout_centerVertical="true"
        android:background="@drawable/own_view_press_selector"
        android:textColor="@android:color/white"
        android:text="返回"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/own_two_txt"
        android:text="标题"
        android:layout_centerInParent="true"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/own_three_txt"
        android:textSize="14sp"
        android:layout_marginRight="10dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white"
        android:padding="5dp"
        android:background="@drawable/own_view_press_selector"
        android:text="设置"/>
</RelativeLayout></span>

效果图如下:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值