自定义ViewGroup控件--自定义属性(面板思想)

attrs.xml(第一步,在这里)

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<!-- R.styleable.TopBar_titleTextColor在R文件中是这样的形式 -->
    <declare-styleable name="TopBar">

        <!-- 文字 -->
        <attr name="leftText" format="string" />
        <attr name="rightText" format="string" />
        <attr name="titleText" format="string" />
        <!-- 文字颜色 -->
        <attr name="leftTextColor" format="color" />
        <attr name="rightTextColor" format="color" />
        <attr name="titleTextColor" format="color" />
        <!-- 文字大小 -->
        <attr name="titleTextSize" format="dimension" />
        <!-- 背景色 -->
        <attr name="leftBackground" format="reference|color" />
        <attr name="rightBackground" format="reference|color" />
    </declare-styleable>

</resources>

activity_main.xml(命名空间需要注意)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res/com.example.custometopbarui"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sloop.topbar.MainActivity" >

    <com.example.custometopbarui.TopBar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="48dp"
        app:leftBackground="@drawable/ic_launcher"
        app:leftText="左侧"
        app:rightBackground="@drawable/ic_launcher"
        app:rightText="右侧"
        app:titleText="自定义标题"
        app:titleTextColor="#ffffff"
        app:titleTextSize="8sp" >
    </com.example.custometopbarui.TopBar>

</RelativeLayout>

TopBar(注释很详细)

package com.example.custometopbarui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TopBar extends RelativeLayout {

	/**
	 * 定义三个控件,
	 */
	private Button leftButton, rightButton;
	private TextView title;

	//左侧button属性
	private int leftTextColor;
	private Drawable leftBackground;
	private String leftText;

	//右侧button属性
	private int rightTextColor;
	private Drawable rightBackground;
	private String rightText;

	//title属性
	private int titleTextColor;
	private float titleTextSize;
	private String titleText;

	//因为容器
	private LayoutParams leftParams, rirhtParams, titleParams;

	//定义接口对象
	private TopBarClickListener listener;

	/**
	 * 点击事件监听器接口
	 * @author Administrator
	 *
	 */
	public interface TopBarClickListener {

		public void leftclick();

		public void rightclick();
	}

	//设置监听器
	public void setOnTopBarClickListener(TopBarClickListener listener){
		this.listener = listener;
	}

	/**
	 * 对外暴露的方法
	 * @param visible
	 */
	public void setLeftIsVisible(boolean visible){
		if (visible) {
			leftButton.setVisibility(View.VISIBLE);
		} else {
			leftButton.setVisibility(View.GONE);
		}
	}
	public void setRightIsVisible(boolean visible){
		if (visible) {
			rightButton.setVisibility(View.VISIBLE);
		} else {
			rightButton.setVisibility(View.GONE);
		}
	}

	/**
	 * 构造器
	 * @param context
	 * @param attrs
	 */
	public TopBar(Context context, AttributeSet attrs){

		super(context, attrs);
		//获取自定义属性和值的映射集合
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);

		//取出自定义属性 - 左侧
		leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, Color.BLACK);
		leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
		leftText = ta.getString(R.styleable.TopBar_leftText);

		//取出自定义属性 - 右侧
		rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, Color.BLACK);
		rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
		rightText = ta.getString(R.styleable.TopBar_rightText);

		//取出自定义属性 - 标题
		titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, Color.BLACK);
		titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 12);
		titleText = ta.getString(R.styleable.TopBar_titleText);
		//回收TypedArray(避免浪费资源,避免因为缓存导致的错误)

		ta.recycle();

		/**
		 * 创建控件对象
		 */
		leftButton = new Button(context);
		rightButton = new Button(context);
		title = new TextView(context);

		/**
		 * 为各个控件设置属性 - 左侧
		 */
		leftButton.setText(leftText);
		leftButton.setTextColor(leftTextColor);
		leftButton.setBackground(leftBackground);

		//设置属性 - 右侧
		rightButton.setText(rightText);
		rightButton.setTextColor(rightTextColor);
		rightButton.setBackground(rightBackground);

		//设置属性 - 标题
		title.setText(titleText);
		title.setTextSize(titleTextSize);
		title.setTextColor(titleTextColor);
		title.setGravity(Gravity.CENTER);

		//设置整体背景颜色
		setBackgroundColor(0xfff59563);

		/**
		 * 为各个控件设置布局 - 左
		 */
		//设置宽高
		leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		//添加规则
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
		leftParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
		addView(leftButton, leftParams);//将按钮添加进布局中

		//设置布局 - 右
		rirhtParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		rirhtParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
		rirhtParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
		addView(rightButton, rirhtParams);//将按钮添加进布局中

		//设置布局 - 标题
		titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
		addView(title, titleParams);//将按钮添加进布局中

		/**
		 * 设置监听器
		 */
		leftButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v){

				//这里写一个接口回调方法,让外部去实现即可
				listener.leftclick();
			}
		});
		rightButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v){

				listener.rightclick();
			}
		});
	}

}

MainActivity

package com.example.custometopbarui;

import com.example.custometopbarui.TopBar.TopBarClickListener;

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

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState){

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TopBar topBar = (TopBar) findViewById(R.id.topbar);
		topBar.setLeftIsVisible(false);
//		topBar.setRightIsVisible(false);
		topBar.setOnTopBarClickListener(new TopBarClickListener() {

			@Override
			public void rightclick(){
				Toast.makeText(MainActivity.this, "Right Clicked", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void leftclick(){
				Toast.makeText(MainActivity.this, "Left Clicked", Toast.LENGTH_SHORT).show();
			}
		});
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 13:41:34

自定义ViewGroup控件--自定义属性(面板思想)的相关文章

自定义ViewGroup控件(二)-----&gt;流式布局进阶(二)

main.xml <?xml version="1.0" encoding="utf-8"?> <com.example.SimpleLayout.MyLinLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout

自定义ViewGroup控件(一)-----&gt;流式布局进阶(一)

main.xml <?xml version="1.0" encoding="utf-8"?> <com.example.SimpleLayout.MyLinLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout

自定义组合控件和在自定义控件中使用自定义属性

今天,整理了一下我平时的笔记,写一个比较简单的自定义组合控件,仅供小白参考,大神请绕道,希望能够对大家有一些帮助 首先,得明白为什么我们需要自定义组合控件,它是因为原有控件并不能满足开发的需求,或者说并不能达到我们想要的一种效果,这个时候,就需要我们自己定义一些控件,以达到目的 ![先来看一下效果](http://img.blog.csdn.net/20160716224219109) 个人总结自定义控件的步骤: 1.先写一个布局,这里我用的是一个相对布局,我这里的相对布局就是根布局了 <?xm

android 自定义组合控件

自定义控件是一些android程序员感觉很难攻破的难点,起码对我来说是这样的,但是我们可以在网上找一些好的博客关于自定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲下自定义组合控件,这个特别适合在标题栏或者设置界面,看下面图: 就非常适合使用组合控件了,现在写一个玩玩: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Android自定义控件——自定义组合控件

转载请注明出处http://blog.csdn.net/allen315410/article/details/39581055  前面几篇博文介绍了Android如何自定义控件,其实就是讲一下如何"从无到有"的自定义一个全新的控件,继承View或者继承ViewGroup,复写其相关方法,这种自定义控件的方式相对来说难度较大,而且并不是所有需要新控件的情况下,都要这样进行.有很多情况下,我们只要运用好Android给我提供好的控件,经过布局巧妙的结合在一起,就是一个新的控件,我称之为&

Android自定义控件之自定义组合控件(三)

前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发成本,以及维护成本. 使用自定义组合控件的好处? 我们在项目开发中经常会遇见很多相似或者相同的布局,比如APP的标题栏,我们从三种方式实现标题栏来对比自定义组件带来的好处,毕竟好的东西还是以提高开发效率,降低开发成本为导向的. 1.)第一种方式:直接在每个xml布局中写相同的标题栏布局代码 <?xm

自定义组合控件,适配器原理-Day31

自定义组合控件,适配器原理-Day31 mobile2.1 主页定义 手机上锁功能 1.弹出设置密码框. 手机下载进度 自定定义控件 控件的属性其实就是控件类一个属性设置属性调用类的set方法方法, 自定义组合控件的思路 生命一个View对象继承自相对布局,线性布局或者其他的ViewGroup 在View对象重写构造方法,然后初始化布局,通过View.inflate()方法把我们自己定义的布局挂到界面当中. 自定义属性在res/values目录下创建attrs.xml里面定义一些属性, <res

第十六天 自定义控件和自定义组合控件

1.  setContentView() 一旦调用,layout 会立即显示UI 2. inflate 只会将layout 形成一个以view类 实现 的对象 ,需要显示的时候还需要调用 setContentView() . ---------------------------------------------------------------------------------------------- 自定义控件组合 第一步 :先写要组合的一些需要的控件,将其封装到一个布局xml布局文

自定义组合控件的方法

自定义组合控件 1)编写一个类继承ViewGroup, 2)重写构造方法 3)在XML中配置一个视图,控件初始化时,填充这个视图,并挂载到控件中 4)添加自定义属性 在value目录中,编写一个xml文件,<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="setting_view_style"> <att