隐藏ActionBar,可以自定义一个组合控件替代
组合控件
组合控件,顾名思义就是将一些小的控件组合起来形成一个新的控件,这些小的控件多是系统自带的控件。比如很多应用中普遍使用的标题栏控件,其实用的就是组合控件,那么下面将通过实现一个简单的标题栏自定义控件来说说组合控件的用法。
1、创建自定义标题栏的布局文件activitiy_app_title.xml:
<?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="@color/colorTheme" android:focusable="true" android:focusableInTouchMode="true"> <ImageView android:id="@+id/img_back" android:layout_width="wrap_content" android:layout_height="50dp" android:contentDescription="@null" android:paddingEnd="@dimen/mdp_15" android:paddingLeft="@dimen/mdp_10" android:paddingRight="@dimen/mdp_15" android:paddingStart="@dimen/mdp_10" android:scaleType="fitCenter" android:src="@mipmap/icon_back" android:visibility="invisible" /> <TextView android:id="@+id/tv_title_mid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center_horizontal" android:textColor="@color/colorWhite" android:textSize="@dimen/msp_16" /> <ImageView android:id="@+id/img_right" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:contentDescription="@null" android:paddingEnd="@dimen/mdp_10" android:paddingLeft="@dimen/mdp_15" android:paddingRight="@dimen/mdp_10" android:paddingStart="@dimen/mdp_15" android:scaleType="fitCenter" android:src="@mipmap/icon_search" android:visibility="invisible" /> <TextView android:id="@+id/tv_title_right" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:gravity="center_vertical" android:paddingEnd="@dimen/mdp_10" android:paddingLeft="@dimen/mdp_15" android:paddingRight="@dimen/mdp_10" android:paddingStart="@dimen/mdp_15" android:textColor="@color/colorWhite" android:textSize="@dimen/msp_14" android:visibility="invisible" /> </RelativeLayout>
2、在res/values/ 下建立一个attrs.xml
(一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;不清楚的可以google一把。)
<?xml version="1.0" encoding="UTF-8"?> <resources> <declare-styleable name="TitleLinearLayout"> <attr name="isBack" format="boolean" /> </declare-styleable> </resources>
3、创建一个类TitleView,继承自LinearLayout:
package com.a.b.view; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.a.b.R; /** * Title通用布局 * * @author Administrator */ public class MTitle extends LinearLayout { private ImageView imgBack; private TextView tvMid; private ImageView imgRight; private TextView tvRight; public MTitle(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context, attrs); } public MTitle(Context context, AttributeSet attrs) { super(context, attrs); initView(context, attrs); } private void initView(final Context context, AttributeSet attrs) { View title = LayoutInflater.from(context).inflate(R.layout.app_title, null); tvMid = (TextView) title.findViewById(R.id.tv_title_mid); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleLinearLayout); boolean isBack = a.getBoolean(R.styleable.TitleLinearLayout_isBack, false); if (getTag() != null) { tvMid.setText(this.getTag().toString()); } imgBack = (ImageView) title.findViewById(R.id.img_back); imgRight = (ImageView) title.findViewById(R.id.img_right); tvRight = (TextView) title.findViewById(R.id.tv_title_right); imgBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity) context).finish(); } }); if (isBack) { imgBack.setVisibility(View.VISIBLE); } title.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); this.addView(title); } public void setTitle(String str) { if (str != null) { tvMid.setText(str); } } public String getTitle() { return tvMid.getText().toString(); } public ImageView getTitleRigthImg(int res) { tvRight.setVisibility(View.INVISIBLE); imgRight.setVisibility(View.VISIBLE); imgRight.setImageResource(res); return imgRight; } public ImageView getTitleLeftImg(int res) { imgBack.setVisibility(View.VISIBLE); imgBack.setImageResource(res); return imgBack; } public TextView getTitleRightText(String str) { imgRight.setVisibility(View.INVISIBLE); tvRight.setVisibility(View.VISIBLE); tvRight.setText(str); return tvRight; } }
4、在.xml中引入自定义的标题栏:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:title="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorbackhui" android:orientation="vertical"> <com.youpinwallet.ypw.view.MTitle android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/mdp_10" android:tag="我的优惠券" title:isBack="true" /> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/x_recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mdp_6" android:layout_marginRight="@dimen/mdp_6" android:paddingBottom="@dimen/mdp_10" android:paddingTop="@dimen/mdp_5" android:visibility="gone" android:background="@color/colorbackhui" /> </LinearLayout>
5、在java代码中引用标题栏
title.setTitle(type.equals("WD") ? "我的优惠券" : "可使用优惠券"); title.getTitleRightText("使用规则").setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(CouponActivity.this, WebViewActivity.class); intent.putExtra("title", "优惠券使用规则"); intent.putExtra("url", ApiUtils.URL_COUPON_DEAL); startActivity(intent); } });
自绘控件
自绘控件的内容都是自己绘制出来的,在View的onDraw方法中完成绘制。
1、组合控件中2自定义属性;
2、组合控件中3;
重写三个构造方法;
重写onDraw,onMesure调用系统提供的:
3、组合控件中4引用到布局中;
继承控件
就是继承已有的控件,创建新控件,保留继承的父控件的特性,并且还可以引入新特性。