zoukankan      html  css  js  c++  java
  • Android 自定义控件之app标题栏的封装

    在app的开发中,每一个页面都有上面的标题栏,总不能在开发的过程中没个界面都写一个标题栏的布局,所以为了开发的方便,将该标题栏进行的封装,以后在实际的开发工作中,也可以将该封装好的标题栏控件直接拿来使用,当然肯定是需要更改里面的布局文件的

     

    下面贴出的是布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#F34B4E"
        android:gravity="center_vertical" >
    
        <LinearLayout
            android:id="@+id/llLeftGoBack"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@drawable/widget_titlebar_selector"
            android:clickable="true"
            android:focusable="true"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="15dp" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:clickable="false"
                android:drawableLeft="@drawable/myapptitle_back"
                android:drawablePadding="5dp"
                android:focusable="false"
                android:gravity="center"
                android:text="返回"
                android:textColor="#FFFFFF"
                android:textSize="16sp" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/llRight"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/widget_titlebar_selector"
            android:clickable="true"
            android:focusable="true"
            android:orientation="horizontal"
            android:paddingLeft="15dp"
            android:paddingRight="15dp" >
    
            <ImageView
                android:id="@+id/ivRightComplete"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:clickable="false"
                android:focusable="false"
                android:visibility="gone" />
    
            <TextView
                android:id="@+id/tvRightComplete"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:clickable="false"
                android:focusable="false"
                android:gravity="center"
                android:text="完成"
                android:textColor="#FFFFFF"
                android:textSize="16sp"
                android:visibility="visible" />
        </LinearLayout>
    
        <TextView
            android:id="@+id/tvCenterTitle"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_toRightOf="@id/llLeftGoBack"
            android:layout_toLeftOf="@id/llRight"
            android:ellipsize="end"
            android:gravity="center"
            android:singleLine="true"
            android:text="标题"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />
    
    </RelativeLayout>

    下面是贴出来封装好的类

    package com.renrui.job.widget;
    
    import com.renrui.job.R;
    import com.renrui.job.util.Utility;
    
    import android.annotation.TargetApi;
    import android.content.Context;
    import android.os.Build;
    import android.text.TextUtils;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    /**
     * My app title
     **/
    public class MyAppTitle extends LinearLayout
    {
        private OnLeftButtonClickListener mLeftButtonClickListener;
        private OnRightButtonClickListener mRightButtonClickListener;
        private MyViewHolder mViewHolder;
        private View viewAppTitle;
    
        public MyAppTitle(Context context)
        {
            super(context);
    
            init();
        }
    
        public MyAppTitle(Context context, AttributeSet attrs)
        {
            super(context, attrs);
    
            init();
        }
    
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public MyAppTitle(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
    
            init();
        }
    
        private void init()
        {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            viewAppTitle = inflater.inflate(R.layout.view_activity_titlebar, null);
            this.addView(viewAppTitle, layoutParams);
    
            mViewHolder = new MyViewHolder(this);
            mViewHolder.llLeftGoBack.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    if (Utility.isFastDoubleClick())
                    {
                        return;
                    }
                    
                    if (mLeftButtonClickListener != null)
                    {
                        mLeftButtonClickListener.onLeftButtonClick(v);
                    }
                }
            });
            mViewHolder.llRight.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    if (Utility.isFastDoubleClick())
                    {
                        return;
                    }
                    
                    if (mRightButtonClickListener != null)
                    {
                        mRightButtonClickListener.OnRightButtonClick(v);
                    }
                }
            });
        }
    
        public void initViewsVisible(boolean isLeftButtonVisile, boolean isCenterTitleVisile, boolean isRightIconVisile, boolean isRightTitleVisile)
        {
            // 左侧返回
            mViewHolder.llLeftGoBack.setVisibility(isLeftButtonVisile ? View.VISIBLE : View.INVISIBLE);
    
            // 中间标题
            mViewHolder.tvCenterTitle.setVisibility(isCenterTitleVisile ? View.VISIBLE : View.INVISIBLE);
    
            // 右侧返回图标,文字
            if (!isRightIconVisile && !isRightTitleVisile)
            {
                mViewHolder.llRight.setVisibility(View.INVISIBLE);
            }
            else
            {
                mViewHolder.llRight.setVisibility(View.VISIBLE);
            }
            mViewHolder.ivRightComplete.setVisibility(isRightIconVisile ? View.VISIBLE : View.GONE);
            mViewHolder.tvRightComplete.setVisibility(isRightTitleVisile ? View.VISIBLE : View.INVISIBLE);
        }
    
        /**
         * 设置标题
         * 
         * @param title
         */
        public void setAppTitle(String title)
        {
            if (!TextUtils.isEmpty(title))
            {
                mViewHolder.tvCenterTitle.setText(title);
            }
        }
    
        public void setRightTitle(String text)
        {
            if (!TextUtils.isEmpty(text))
            {
                mViewHolder.tvRightComplete.setText(text);
            }
        }
    
        public void setRightIcon(int sourceID)
        {
            mViewHolder.ivRightComplete.setImageResource(sourceID);
        }
    
        public void setLeftOnclick(OnLeftButtonClickListener mOnLeftButtonClickListener)
        {
            if (mOnLeftButtonClickListener != null)
            {
            }
        }
    
        public void setAppBackground(int color)
        {
            viewAppTitle.setBackgroundColor(color);
        }
    
        public void setOnLeftButtonClickListener(OnLeftButtonClickListener listen)
        {
            mLeftButtonClickListener = listen;
        }
    
        public void setOnRightButtonClickListener(OnRightButtonClickListener listen)
        {
            mRightButtonClickListener = listen;
        }
    
        public static abstract interface OnLeftButtonClickListener
        {
            public abstract void onLeftButtonClick(View v);
        }
    
        public static abstract interface OnRightButtonClickListener
        {
            public abstract void OnRightButtonClick(View v);
        }
    
        static class MyViewHolder
        {
            LinearLayout llLeftGoBack;
            TextView tvCenterTitle;
            LinearLayout llRight;
            ImageView ivRightComplete;
            TextView tvRightComplete;
    
            public MyViewHolder(View v)
            {
                llLeftGoBack = (LinearLayout) v.findViewById(R.id.llLeftGoBack);
                tvCenterTitle = (TextView) v.findViewById(R.id.tvCenterTitle);
                llRight = (LinearLayout) v.findViewById(R.id.llRight);
                ivRightComplete = (ImageView) v.findViewById(R.id.ivRightComplete);
                tvRightComplete = (TextView) v.findViewById(R.id.tvRightComplete);
            }
        }
    }

    在使用中如何使用呢,接下来看在项目中  布局文件的引用

        <包名.MyAppTitle
            android:id="@+id/myNewAppTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </包名.MyAppTitle>

    下面是在activity中如何初始化该标题的代码

    private void setMyAppTitle()
        {
            MyAppTitle mNewAppTitle = (MyAppTitle) this.findViewById(R.id.myNewAppTitle);
    
            mNewAppTitle.initViewsVisible(true, true, false, true);
    
            mNewAppTitle.setAppTitle(getString(R.string.toptitle_tool_MySuggestActivity));
            mNewAppTitle.setRightTitle("提交");
            mNewAppTitle.setOnLeftButtonClickListener(new OnLeftButtonClickListener()
            {
                @Override
                public void onLeftButtonClick(View v)
                {
                    onBackPressed();
                }
            });
    
            mNewAppTitle.setOnRightButtonClickListener(new OnRightButtonClickListener()
            {
                @Override
                public void OnRightButtonClick(View v)
                {
                    //TODO
                }
            });
        }

    这就是全部的代码了 在这里贴出来  方便以后的学习

  • 相关阅读:
    CSS 中 Font-Family 中英文对照表
    0号首页,移动应用快捷门户
    身边的大数据和我们的着手点
    php单例模式
    原生JS实现ajax与ajax的跨域请求
    two.js之实现动画效果
    H5中的拖放
    简说chart2.4的应用,以及Uncaught ReferenceError : require is not defined的解决
    JS中的作用域以及全局变量的问题
    JavaScript 框架------------AngularJS(下)
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/6222816.html
Copyright © 2011-2022 走看看