Android如何使用自定义控件

实现一个如图所示的自定义控件


实现自定义控件的步骤:

  1. 自定义属性(attr.xml)
  2. 自定义控件(Tobbar.java)
  3. 引用自定义控件activity_main.xml)
  4. 主程序调用(MainActivity.java)

1.自定义属性(attr.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- declare-styleable:自定义属性的值 -->
    <declare-styleable name="TopBar">
        <attr name="title" format="string" />
        <attr name="titleTextSize" format="dimension" />
        <attr name="titleTextColor" format="color" />
        <attr name="leftTextColor" format="color" />
        <attr name="leftBackground" format="reference|color" />
        <attr name="leftText" format="string" />
        <attr name="rightTextColor" format="color" />
        <attr name="rightBackground" format="reference|color" />
        <attr name="rightText" format="string" />
    </declare-styleable>
</resources>
自定义属性 详解

2.自定义控件(Tobbar.java)
package com.sky.customwidget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class Topbar extends RelativeLayout {
	private Button leftButton, rightButton;
	private TextView tvTitle;

	private int leftTextColor;
	private Drawable leftBackground;
	private String leftText;

	private int rightTextColor;
	private Drawable rightBackground;
	private String rightText;

	private float titleTextSize;
	private int titleTextColor;
	private String title;
        private LayoutParams leftParams, rightParams, titleParams;


	public Topbar(Context context, AttributeSet attrs) {
		super(context, attrs);
		//使用TypedArray存储获取到的自定义属性,查看View的源代码3281行

		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.Topbar);
		leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
		leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
		leftText = ta.getString(R.styleable.Topbar_leftText);

		rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
		rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
		rightText = ta.getString(R.styleable.Topbar_rightText);

		titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
		titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
		title = ta.getString(R.styleable.Topbar_skytitle);

		ta.recycle();
		leftButton = new Button(context);
		rightButton = new Button(context);
		tvTitle = new TextView(context);

		leftButton.setTextColor(leftTextColor);
		leftButton.setBackgroundDrawable(leftBackground);
		leftButton.setText(leftText);

		rightButton.setTextColor(rightTextColor);
		rightButton.setBackgroundDrawable(rightBackground);
		rightButton.setText(rightText);

		tvTitle.setTextColor(titleTextColor);
		tvTitle.setTextSize(titleTextSize);
		tvTitle.setText(title);
		tvTitle.setGravity(Gravity.CENTER);

		setBackgroundColor(0xFFF59563);

		leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.FILL_PARENT);
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		addView(leftButton, leftParams);

		rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.FILL_PARENT);
		rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		addView(rightButton, rightParams);

		titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		titleParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		addView(tvTitle, titleParams);
        }
}
这里我们继承了RelativeLayout,调用了一部分RelativeLayout的属性,要想实现更加自由的布局,可以直接继承View类

3.引用自定义控件activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:sky="http://schemas.android.com/apk/res/com.sky.customwidget"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.sky.customwidget.Topbar 
        android:id="@+id/topBar"
        android:layout_width="fill_parent"
    	android:layout_height="40dp"
    	sky:leftBackground="#323412"
	sky:leftText="Back"
	sky:leftTextColor="#FFFFFF"
	sky:rightBackground="#123f12"
	sky:rightText="More"
	sky:rightTextColor="#FFFFFF"
	sky:skytitle="Title"
	sky:titleTextColor="#123412"
	sky:titleTextSize="10sp"></com.sky.customwidget.Topbar>

</RelativeLayout>
注意:要定义 xmlns:sky="http://schemas.android.com/apk/res/包名",之后才能写sky:XXX
    而且这里的XXX要与attr.xml里定义的名称一一对应

4.主程序调用(MainActivity.java)
package com.sky.customwidget;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}


}

这样,我们的自定义控件就完成了,可是如果我们想要将这个自定义控件封装成jar包,并实现左右Button的单击事件该怎么做呢?
自定义控件进阶(请听下回分解~~)


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值