Android View之UI模板
来源:互联网 发布:h5互动游戏 源码 编辑:程序博客网 时间:2024/04/28 19:54
1. 设计需要的属性values文件夹中创建attrs.xml的文件,在<resource>的tag内添加我们所需要的属性声明若编译时属性有冲突,更改名字<resources>
<!-- reference 代表drawable对象-->
<declare-styleable name="Topbar">
<attr name="centerTitle" format="string"/>
<attr name="centerTitleTextSize" format="dimension"/>
<attr name="centerTitleTextColor" 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. 创建一个实现view的类
package com.hc.test;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.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;/** * Created by Administrator on 2016/8/22. */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 centerTitleTextSize; private int centerTitleTextColor; private String centerTitle; private LayoutParams leftParams, rightParams; private LayoutParams titleParams; // 定义监听事件接口 public interface TopbarClickListener{ void onLeftClick(); void onRightClick(); } private TopbarClickListener listener; public void setOnTopbarClickLister(TopbarClickListener listener){ this.listener = listener; } public Topbar(Context context, AttributeSet attrs) { super(context, attrs); // 该方法获取在xml中定义的属性 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); centerTitleTextSize = ta.getDimension(R.styleable.Topbar_centerTitleTextSize, 0); centerTitleTextColor = ta.getColor(R.styleable.Topbar_centerTitleTextColor, 0); centerTitle = ta.getString(R.styleable.Topbar_centerTitle); ta.recycle(); // 回收 leftButton = new Button(context); rightButton = new Button(context); tvTitle = new TextView(context); leftButton.setTextColor(leftTextColor); leftButton.setBackground(leftBackground); leftButton.setText(leftText); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightBackground); rightButton.setText(rightText); tvTitle.setText(centerTitle); tvTitle.setTextColor(centerTitleTextColor); tvTitle.setTextSize(centerTitleTextSize); tvTitle.setGravity(Gravity.CENTER); setBackgroundColor(0xFFF59563); // 定义子控件的参数,并添加到父控件中 leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); addView(leftButton, leftParams); rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); addView(rightButton, rightParams); titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); addView(tvTitle, titleParams); // button的事情采用传进来的回调函数 leftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(listener != null){ listener.onLeftClick(); } } }); rightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(listener != null){ listener.onRightClick(); } } }); }}
3. xml中引用view
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" 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="com.hc.test.MainActivity"> <com.hc.test.Topbar android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="40dp" custom:leftBackground="@drawable/round_blue_btn" custom:leftText="Back" custom:leftTextColor="#FFFFFF" custom:rightBackground="@drawable/round_blue_btn" custom:rightText="GO" custom:rightTextColor="#FFFFFF" custom:centerTitle="自定义标题" custom:centerTitleTextColor="#235622" custom:centerTitleTextSize="10sp" > </com.hc.test.Topbar></RelativeLayout>注意:xmlns:xml namespace的意思,告诉我们如何去解析属性引用第三方名字空间,我们需要加上如下这句
xmlns:custom="http://schemas.android.com/apk/res-auto"我们定义的属性则都以custom:开头效果图:
0 0
- Android View之UI模板
- 自定义View--------Android UI模板设计
- 自定义View----Android UI模板设计
- Android UI绘制之View重绘
- Android UI详解之View绘制原理
- Android Ui之Recycle View学习
- UI之自定义View
- 【Android实战之旅 003】Android UI模板设计
- android UI之View和ViewGroups简介
- Android UI 绘制机制之View创建过程
- Android UI之View的加载机制(二)
- android UI 优化之<viewstub>实现View的延迟加载
- Android之为什么只能在UI线程操作View
- Android基础——4:UI之View
- Android之为什么只能在UI线程操作View
- android UI设计模板
- Android UI 模板设计
- Android UI模板设计
- Android通知栏微技巧,那些你所没关注过的小细节
- C语言再学习 -- 浮点数
- 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求
- Android控件 - View 简介
- SDWebImage报Too many arguments to function call, expected 0, have 5
- Android View之UI模板
- 工具类--BitmapUtil图片转换,图片缩放,旋转图片
- TreeView控件加载一般方法和把参数传回父窗体
- MySQL练习2
- JAVA中获取当前系统时间
- 轻松把玩HttpClient之封装HttpClient工具类(六),封装输入参数,简化工具类
- Android控件 - TextView、Button、EditText、CompoundButton、CheckBox简介
- 常见HTTP状态(304,200等)
- eclipse中添加Json Editor Plugin 插件的方法