Android--自定义标题栏(TopBar)

来源:互联网 发布:河南大学二本专业知乎 编辑:程序博客网 时间:2024/06/04 18:36

使用(以标题属性为例)

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:topbar="http://schemas.android.com/apk/res-auto"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <com.*.*.widght.TopBar        android:layout_width="match_parent"        android:layout_height="wrap_content"        topbar:titleBarTitle ="@string/forget_name"        >    </com.*.*.widght.TopBar></RelativeLayout>

包含status的24dp和标题栏的56dp

dimen

    <dimen name="activity_margin_small">4dp</dimen>    <dimen name="dimenStatusBarHeight">24dp</dimen>    <dimen name="dimenTitleBarHeight">56dp</dimen>    <dimen name="dimenTopBarHeight">80dp</dimen>    <!--标题栏相关-->    <dimen name="dimenTiltBarTitleSize">20sp</dimen>    <dimen name="dimenTitleBarImageSize">24dp</dimen>    <dimen name="dimenTitleBarImageLayoutSize">48dp</dimen>

TopBar布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/root"    android:layout_width="match_parent"    android:layout_height="@dimen/dimenTopBarHeight"    android:orientation="vertical"    tools:ignore ="all"    >    <View        android:layout_width="match_parent"        android:layout_height="@dimen/dimenStatusBarHeight"        android:background="@color/colorStatusBar"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="@dimen/dimenTitleBarHeight"        android:orientation="horizontal"        android:background="@color/colorTitleBar"        android:padding="@dimen/activity_margin_small"        >        <RelativeLayout            android:id="@+id/left_layout"            style="@style/styleTitleBarImageLayout"            android:paddingRight="@dimen/activity_margin_small"            android:paddingEnd="@dimen/activity_margin_small"            >            <ImageView                android:id="@+id/left_image"                style="@style/styleTitleBarImage"/>        </RelativeLayout>        <TextView            android:id="@+id/title"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:textColor="@color/colorTextPrimary"            android:layout_gravity="center"            android:gravity="center"            android:textSize="@dimen/dimenTiltBarTitleSize" />        <RelativeLayout            android:id="@+id/right_layout"            style="@style/styleTitleBarImageLayout"            android:paddingLeft="@dimen/activity_margin_small"            android:paddingStart="@dimen/activity_margin_small"            >            <ImageView                android:id="@+id/right_image"                style="@style/styleTitleBarImage" />        </RelativeLayout>    </LinearLayout></LinearLayout>

TopBar自定义属性

    <!--自定义标题栏-->    <declare-styleable name="TopBar">        <attr name="titleBarTitle" format="string"/>        <attr name="titleBarTitleBg" format="reference"/>        <attr name="titleBarLeftImage" format="reference"/>        <attr name="titleBarRightImage" format="reference"/>        <attr name="titleBarBackground" format="reference|color"/>    </declare-styleable>

TopBar组件代码

/** * Created by TpOut on 2016/9/28. * Email: 416756910@qq.com */public class TopBar extends LinearLayout{    protected RelativeLayout leftLayout;    protected ImageView leftImage;    protected RelativeLayout rightLayout;    protected ImageView rightImage;    protected TextView titleView;    protected LinearLayout titleLayout;    public TopBar(Context context, AttributeSet attrs, int defStyle) {        this(context, attrs);    }    public TopBar(Context context, AttributeSet attrs) {        super(context, attrs);        init(context, attrs);    }    public TopBar(Context context) {        super(context);        init(context, null);    }    private void init(Context context, AttributeSet attrs){        LayoutInflater.from(context).inflate(R.layout.widght_title_bar, this);        leftLayout = (RelativeLayout) findViewById(R.id.left_layout);        leftImage = (ImageView) findViewById(R.id.left_image);        rightLayout = (RelativeLayout) findViewById(R.id.right_layout);        rightImage = (ImageView) findViewById(R.id.right_image);        titleView = (TextView) findViewById(R.id.title);        titleLayout = (LinearLayout) findViewById(R.id.root);        parseStyle(context, attrs);    }    private void parseStyle(Context context, AttributeSet attrs){        if(attrs != null){            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);            String title = ta.getString(R.styleable.TopBar_titleBarTitle);            if(null != title){                titleView.setText(title);            }            Drawable titleBgDrawable = ta.getDrawable(R.styleable.TopBar_titleBarTitleBg);            if(null != titleBgDrawable){                titleView.setBackground(titleBgDrawable);            }            Drawable leftDrawable = ta.getDrawable(R.styleable.TopBar_titleBarLeftImage);            if (null != leftDrawable) {                leftImage.setImageDrawable(leftDrawable);            }            Drawable rightDrawable = ta.getDrawable(R.styleable.TopBar_titleBarRightImage);            if (null != rightDrawable) {                rightImage.setImageDrawable(rightDrawable);            }            Drawable background = ta.getDrawable(R.styleable.TopBar_titleBarBackground);            if(null != background) {                titleLayout.setBackground(background);            }            ta.recycle();        }    }    public void setLeftImageResource(int resId) {        leftImage.setImageResource(resId);    }    public void setRightImageResource(int resId) {        rightImage.setImageResource(resId);    }    public void setLeftLayoutClickListener(View.OnClickListener listener){        leftLayout.setOnClickListener(listener);    }    public void setRightLayoutClickListener(View.OnClickListener listener){        rightLayout.setOnClickListener(listener);    }    public void setLeftLayoutVisibility(int visibility){        leftLayout.setVisibility(visibility);    }    public void setRightLayoutVisibility(int visibility){        rightLayout.setVisibility(visibility);    }    public void setTitle(String title){        titleView.setText(title);    }    public void setBackgroundColor(int color){        titleLayout.setBackgroundColor(color);    }    public RelativeLayout getLeftLayout(){        return leftLayout;    }    public RelativeLayout getRightLayout(){        return rightLayout;    }}
0 0