Android学习之自定义view——TopBar
来源:互联网 发布:ubuntu trusty xenial 编辑:程序博客网 时间:2024/05/16 19:06
每个软件都有属于自的TopBar,比如QQ,微信。。。。。
今天我们就来简单讲解一下自定义的TopBar的做法。
首先先来一张效果图:
一般的TopBar都有三个属性,一个左Button、右Button,还有就是中间的标题。
然后每个文字都会有各自的属性,字体大小,字体颜色,背景等等。。。
所以这次,我们先把最基础的属性将他配置上
先在value文件下新建attrs.xml文件
<declare-styleable name="TopBar"> <!-- 标题属性 --> <attr name="titleText" format="string"/> <attr name="titleTextSize" format="dimension"/> <attr name="titleTextColor" format="color"/> <attr name="titleBackground" format="reference|color"/> <!-- 左按钮属性 --> <attr name="leftText" format="string"/> <attr name="leftBackground" format="reference|color"/> <attr name="leftTextCloor" format="color"/> <!-- 右按钮属性 --> <attr name="rightText" format="string"/> <attr name="rightBackground" format="reference|color"/> <attr name="rightTextColor" format="color"/> </declare-styleable>
format是值该属性的取值类型,一共有:
string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
基础的属性弄好之后,想想一般的button还是需要点击事件的处理,所以下面的代码已经做了处理,对外暴露出左右按钮的点击事件:
package com.jp.view;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;import com.jp.activity.R;/** * Created by Administrator on 2015/9/3. */public class TopBar extends RelativeLayout{ /** * torbar的左右两个按钮 */ private Button leftButton; private Button rightButton; /** * torbar的标题 */ private TextView title; /** * torbar的左按钮的字体颜色 */ private int leftTextColor; /** * torbar的左按钮的背景 */ private Drawable leftDrawable; /** * torbar的左按钮的标题 */ private String leftText; /** * torbar的右按钮的字体颜色 */ private int rightTextColor; /** * torbar的右按钮的背景 */ private Drawable rightDrawable; /** * torbar的右按钮的标题 */ private String rightText; /** * torbar的标题字体大小 */ private float titleTextSize; /** * torbar的标题字体贪色 */ private int titleTextColor; /** * torbar的标题 */ private String titleText; private Drawable titleBrackgroud; private RelativeLayout.LayoutParams leftParams, rightParams, titleParams; private TopBarClickListener listener; //点击事件监听器接口 public interface TopBarClickListener { public void leftclick(); public void rightclick(); } public TopBar(Context context, AttributeSet attrs) { super(context, attrs); //获取自定义属性和值的映射集合 TypedArray td = context.obtainStyledAttributes(attrs, R.styleable.TopBar); //取出自定义属性 - 左侧 leftTextColor = td.getColor(R.styleable.TopBar_leftTextCloor, Color.WHITE); leftDrawable = td.getDrawable(R.styleable.TopBar_leftBackground); leftText = td.getString(R.styleable.TopBar_leftText); //取出自定义属性 - 右侧 rightTextColor = td.getColor(R.styleable.TopBar_rightTextColor, Color.BLACK); rightDrawable = td.getDrawable(R.styleable.TopBar_rightBackground); rightText = td.getString(R.styleable.TopBar_rightText); //取出自定义属性 - 标题 titleTextSize = td.getDimension(R.styleable.TopBar_titleTextSize, 12); titleText = td.getString(R.styleable.TopBar_titleText); titleTextColor = td.getColor(R.styleable.TopBar_titleTextColor, Color.BLACK); titleBrackgroud = td.getDrawable(R.styleable.TopBar_titleBackground); //回收TypedArray(避免浪费资源,避免因为缓存导致的错误) td.recycle(); leftButton = new Button(context); rightButton = new Button(context); title = new Button(context); //设置属性 - 左侧 leftButton.setText(leftText); leftButton.setTextColor(leftTextColor); leftButton.setBackground(leftDrawable);//设置属性 - 右侧 rightButton.setText(rightText); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightDrawable);//设置属性 - 标题 title.setText(titleText); title.setTextColor(titleTextColor); title.setTextSize(titleTextSize); title.setBackground(titleBrackgroud); title.setGravity(Gravity.CENTER);//设置整体背景颜色 setBackgroundColor(0xffeb933e);//设置布局 - 左 leftParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); leftParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); addView(leftButton, leftParams); //设置布局 - 右 rightParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); rightParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); addView(rightButton, rightParams); //设置布局 - 标题 titleParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); 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(); } }); } //是否显示左右按钮 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); } }}
接下来看布局文件,这里有一点是要提一下
android studio一定要引入
xmlns:custom=”http://schemas.android.com/apk/res-auto”
而ecplice一定要引入
xmlns:custom=”http://schemas.android.com/apk/res/com.example.customview01”我们的命名空间,后面的包路径指的是项目的package
<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:background="#ffffff"tools:context=".MainActivity"> <com.jp.view.TopBar android:background="#eb933e" android:id="@+id/topbar_title" android:layout_width="match_parent" android:layout_height="40dp" custom:leftText="左菜单" custom:leftBackground="#eb933e" custom:rightBackground="#00eb933e" custom:leftTextCloor="#ffffff" custom:rightText="右菜单" custom:rightTextColor="#ffffff" custom:titleText="我是自定义的" custom:titleTextColor="#000000" custom:titleTextSize="10sp" /> </RelativeLayout>
最后在MainActivity中使用的方法:
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(); } }); }}
0 0
- Android学习之自定义view——TopBar
- Android自定义可复用View——topbar
- Android 自定义View:TopBar
- [android进阶]自定义View之TopBar
- android-->自定义view,TopBar模板
- Android 自定义view实现TopBar
- Android学习(十七)自定义View控件 TopBar
- Android——自定义TopBar(ActionBar)
- Android中的接口回调机制(自定义View——TopBar讲解)
- 自定义view:topBar
- 自定义View(TopBar)
- android群英传学习笔记-自定义TopBar
- 关于android自定义控件Topbar的学习
- android基础之自定义控件-topbar
- 自定义View(1)---Topbar
- 自定义控件学习Topbar
- 自定义控件之TopBar
- 自定义控件之-TopBar
- 如何高效学习
- hdu 5425 Rikka with Tree II
- 购物结算功能
- 读书笔记之UNIX环境高级编程(16)
- Centos7 使用总结
- Android学习之自定义view——TopBar
- 阶段性总结(一)
- SQL Server SA 最佳实践(也许不仅仅是翻译)
- IT公司笔试题细节
- SVM支持向量机(一)---简介
- about kvm
- 各种杂项组件(4)之----SearchView(搜索框)、TabHost(选项卡)、ScrollView(滚动视图)
- IOS之Objective-C学习笔记(七)
- 一个我自己写的矩阵快速幂模板