自定义view:topBar
来源:互联网 发布:linux程序 编辑:程序博客网 时间:2024/06/05 23:59
1.values文件夹下新建attrs.xml文件:自定义如下相关属性元素
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="topBar"> <attr name="titleText" format="string"/> <attr name="titleSize" format="dimension"/> <attr name="titleColor" format="color"/> <attr name="leftText" format="string"/> <attr name="leftTextColor" format="color"/> <attr name="leftTextBackground" format="reference|color"/> <attr name="rightText" format="string"/> <attr name="rightTextColor" format="color"/> <attr name="rightTextBackground" format="reference|color"/> </declare-styleable></resources>2.自定义TopBar
package com.example.topbar;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;public class TopBar extends RelativeLayout { private Button leftBtn,rightBtn; private String titleText; private int titleColor; private float titleSize; private String leftText; private Drawable leftBackGround; private int leftTextColor; private String rightText; private Drawable rightBackGround; private int rightTextColor; private LayoutParams leftParams,rightParams,titleParams; public TopBar(Context context, AttributeSet attrs) { super(context, attrs); TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.topBar); titleText = array.getString(R.styleable.topBar_titleText); titleColor = array.getColor(R.styleable.topBar_titleColor,0); titleSize = array.getDimension(R.styleable.topBar_titleSize,0); leftText = array.getString(R.styleable.topBar_leftText); leftBackGround = array.getDrawable(R.styleable.topBar_leftTextBackground); leftTextColor = array.getColor(R.styleable.topBar_leftTextColor,0); rightText = array.getString(R.styleable.topBar_rightText); rightBackGround = array.getDrawable(R.styleable.topBar_rightTextBackground); rightTextColor = array.getColor(R.styleable.topBar_rightTextColor, 0); array.recycle(); leftBtn = new Button(context); leftBtn.setText(leftText); leftBtn.setTextColor(rightTextColor); leftBtn.setBackgroundDrawable(leftBackGround); leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); addView(leftBtn,leftParams); rightBtn = new Button(context); rightBtn.setText(rightText); rightBtn.setTextColor(rightTextColor); rightBtn.setBackgroundDrawable(rightBackGround); rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); addView(rightBtn,rightParams); TextView titleView = new TextView(context); titleView.setText(titleText); titleView.setTextColor(titleColor); titleView.setTextSize(titleSize); titleView.setGravity(Gravity.CENTER); titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); titleParams.addRule(RelativeLayout.CENTER_IN_PARENT); addView(titleView,titleParams); }}
3.应用自定义的view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:my="http://schemas.android.com/apk/res-auto" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.topbar.TopBar android:layout_height="wrap_content" android:layout_width="wrap_content" my:titleText="你好" my:titleSize="15sp" my:titleColor="#ffffd0bf" my:leftTextColor="#ff4290ff" my:leftTextBackground="#ff5fff1b" my:leftText="左按钮" my:rightTextColor="#ff1f1bff" my:rightTextBackground="#ffff1a57" my:rightText="右按钮"/></LinearLayout>4.效果图
0 0
- 自定义view:topBar
- Android 自定义View:TopBar
- 自定义View(TopBar)
- 自定义View(1)---Topbar
- android-->自定义view,TopBar模板
- Android 自定义view实现TopBar
- [android进阶]自定义View之TopBar
- Android自定义可复用View——topbar
- 自定义TopBar
- 自定义TopBar
- 自定义topbar
- 自定义topbar
- 自定义TopBar
- 自定义Topbar
- 自定义TopBar
- Android学习(十七)自定义View控件 TopBar
- Android学习之自定义view——TopBar
- 自定义View-->实现顶部Topbar的简单UI模板
- LeetCode Triangle
- IE6中location不跳转问题
- String、StringBuffer与StringBuilder之间区别
- 架构之重构的12条军规(上)
- TestNG html报告中文乱码解决
- 自定义view:topBar
- HTTP 协议详解
- mysql安装
- HDU 1006 Tick and Tick
- C API 通过ODBC 连接数据库MYSQL
- HelloWorld
- SQL Server 分页存储过程
- linux shell test
- HTML数组详解