android开发标题栏统一管理和自定义属性配置

在android开发中,app每个界面基本上都会有一个标题栏, 并且标题栏的布局大概也都比较相似. 所以我们有时候为了避免过多的重复代码和便于所有的标题统一管理,我们一般会定义一个公共的xml布局文件作为头布局,如果页面使用使用标签引入. 然后再代码中将某些控件findViewById出来,进行监听的设置, 隐藏或者显示设置等等.
今天我们使用一种新的方式统一管理布局,自定义一个布局view并且设置自定义属性.
内容如下:
<一>自定义属性
1,首先在values目录下面建立一个attrs.xml文件, 这里面定义一系列的自定义属性.需要设置属性的名字和属性的类型:例如

<resources>

    <declare-styleable name="TitleBar">
        <!-- rightButton -->
        <attr name="titleColor" format="color" />
        <attr name="titleBackground" format="reference|color" />
        <attr name="titleContent" format="string"/>
        <attr name="titleSize" format="dimension"/>

        <!-- left button -->
        <attr name="leftButtonColor" format="color" />
        <attr name="leftButtonBackground" format="reference|color" />
        <attr name="leftButtonContent" format="string"/>
        <attr name="leftButtonSize" format="dimension"/>

        <!-- right button -->
         <attr name="rightButtonColor" format="color" />
        <attr name="rightButtonBackground" format="reference|color" />
        <attr name="rightButtonContent" format="string"/>
        <attr name="rightButtonSize" format="dimension"/>
    </declare-styleable>

<二> 自定义布局
1,自定义布局继承RelativeLayout这个View容器, 盛放所有的View
2,调用含有传入自定义属性的构造方法 super(context, attrs)

public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.TitleBar);
.............................
}

3,初始化view,设置view的位置,根据自定义view属性的获取他的值,然后设置到对应的view上

<1>查找对应的属性值

titleText = typedArray.getString(R.styleable.TitleBar_titleContent);
        titleSize = typedArray.getDimension(R.styleable.TitleBar_titleSize, 0);
        titleColor = typedArray.getColor(R.styleable.TitleBar_titleColor, 0);
        titleBackground = typedArray
                .getDrawable(R.styleable.TitleBar_titleBackground);

<2>设置view

private void initTitle(Context context) {
        tvTitle = new TextView(context);
        LayoutParams params = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        tvTitle.setText(titleText);
        tvTitle.setTextSize(titleSize);
        tvTitle.setTextColor(titleColor);
        this.setBackground(titleBackground);
        tvTitle.setGravity(Gravity.CENTER);
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        tvTitle.setLayoutParams(params);

        .........

        addView(tvTitle);
    }

<三>定义监听事件
模仿android View.java中的点击事件的处理方式.传入参数int which,标志是哪个view被点击
1,自定义一个内部接口

public interface OnTitleBarClickListener {
        public static final int WHICH_LEFT_BUTTON = 1;
        public static final int WHICH_RIGHT_BUTTON = 2;
        public static final int WHICH_TITLE = 3;

        void onTitleBarClick(int which);
    }

2,提供设置监听的方法和回调方法

    public void setOnTitleBarClickListener(
            OnTitleBarClickListener onTitleBarClickListener) {
        this.onTitleBarClickListener = onTitleBarClickListener;
    }


tvTitle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (onTitleBarClickListener != null) {
                    onTitleBarClickListener.onTitleBarClick(OnTitleBarClickListener.WHICH_TITLE);
                }
            }
        });

<四>关于某个View的显示和隐藏
处理方式传入一个boolean值,true为要显示, false是隐藏

public void setRightVisible(boolean visible) {
        if (rightButton == null) {
            return;
        }

        if (visible) {
            rightButton.setVisibility(View.VISIBLE);
            return;
        }
        rightButton.setVisibility(View.INVISIBLE);
    }

<五>布局xml文件引用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:titlebar="http://schemas.android.com/apk/res/com.easy.titlebar"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <com.easy.titlebar.TitleBar
        android:id="@+id/titlebar"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        titlebar:leftButtonBackground="#00ff00"
        titlebar:leftButtonColor="#ff0000"
        titlebar:leftButtonContent="取消"
        titlebar:leftButtonSize="10sp"
        titlebar:rightButtonBackground="#00ff00"
        titlebar:rightButtonColor="#ff0000"
        titlebar:rightButtonContent="保存"
        titlebar:rightButtonSize="10sp"
        titlebar:titleBackground="#363636"
        titlebar:titleColor="#ff0000"
        titlebar:titleContent="标题内容"
        titlebar:titleSize="20sp" />

</LinearLayout>

1,在引入的时候要注意命名空间.如下.
xmlns:titlebar=”http://schemas.android.com/apk/res/com.easy.titlebar”
模仿android自己的xmlns:android=”http://schemas.android.com/apk/res/android”
在eclipse开发环境下,需要将android换做自己的保命, 命名空间的名字改写为在引用的属性的前缀

2,属性引用 titlebar:leftButtonSize=”10sp”
前缀就是前面引用的命名空间名称: titlebar

资源下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值