Android沉浸式状态栏+图片背景+标题栏渐变+背景伸缩

今天做项目时,刚好用到了一部分Android沉浸式状态栏的技术,我就单独写了出来
该部分代码在PersonView中
首先是自定义ScrollView实现Toolbar(标题栏)渐变

效果展示图:

编程思想:

1、自定义一个类,继承自ScrollView,并重写它的 onScrollChanged 方法;

2、在 onScrollChanged 中获取 ScrollView 在Y轴的移动距离,并根据此距离改变 Toolbar(标题栏) 的透明度。
示例代码:
类:MainActivity.java

package com.example.personview;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.Toast;import com.example.personview.base.BaseActivity;import com.example.personview.impl.ActionBarClickListener;import com.example.personview.widget.TranslucentActionBar;import com.example.personview.widget.TranslucentScrollView;/** * Created by Administrator * time:2019/8/13 */public class MainActivity extends BaseActivity implements ActionBarClickListener, TranslucentScrollView.TranslucentChangedListener, View.OnClickListener {    private TranslucentScrollView translucentScrollView;    private TranslucentActionBar actionBar;    private View zoomView;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();        initClick();//初始化点击事件    }    private void init() {        actionBar = (TranslucentActionBar) findViewById(R.id.actionbar);        //初始actionBar        actionBar.setData("个人中心", 0, null, 0, null, null);        //开启渐变        actionBar.setNeedTranslucent();        //设置状态栏高度        actionBar.setStatusBarHeight(getStatusBarHeight());        translucentScrollView = (TranslucentScrollView) findViewById(R.id.pullzoom_scrollview);        //设置透明度变化监听        translucentScrollView.setTranslucentChangedListener(this);        //关联需要渐变的视图        translucentScrollView.setTransView(actionBar);        zoomView = findViewById(R.id.lay_header);        //关联伸缩的视图        translucentScrollView.setPullZoomView(zoomView);    }    @Override    public void onLeftClick() {    }    @Override    public void onRightClick() {    }    @Override    public void onTranslucentChanged(int transAlpha) {        actionBar.tvTitle.setVisibility(transAlpha > 48 ? View.VISIBLE : View.GONE);    }    /**     * 初始化点击事件     * 获取布局文件toast_signtoday.xml里的控件     **/    private void initClick() {        LinearLayout person_sign = (LinearLayout) findViewById(R.id.person_sign);        person_sign.setOnClickListener(this);        LinearLayout person_footprint = (LinearLayout) findViewById(R.id.person_footprint);        person_footprint.setOnClickListener(this);        LinearLayout person_publish = (LinearLayout) findViewById(R.id.person_publish);        person_publish.setOnClickListener(this);        LinearLayout person_photowall = (LinearLayout) findViewById(R.id.person_photowall);        person_photowall.setOnClickListener(this);        LinearLayout person_lucky = (LinearLayout) findViewById(R.id.person_lucky);        person_lucky.setOnClickListener(this);        LinearLayout person_invite = (LinearLayout) findViewById(R.id.person_invite);        person_invite.setOnClickListener(this);        LinearLayout person_set = (LinearLayout) findViewById(R.id.person_set);        person_set.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.person_sign:                //签到                LayoutInflater inflater = getLayoutInflater();                View layout = inflater.inflate(R.layout.activity_toast_signtoday, null);                Toast toast = new Toast(getApplicationContext());                toast.setGravity(Gravity.CENTER, 0, 0);                toast.setDuration(Toast.LENGTH_LONG);                toast.setView(layout);                toast.show();                break;            case R.id.person_footprint:                //足迹                Toast.makeText(this,"足迹",Toast.LENGTH_SHORT).show();                break;            case R.id.person_publish:                //我发布的                Toast.makeText(this,"我发布的",Toast.LENGTH_SHORT).show();                break;            case R.id.person_photowall:                //照片墙                Toast.makeText(this,"照片墙",Toast.LENGTH_SHORT).show();                break;            case R.id.person_lucky:                //开始抽奖                Toast.makeText(this,"开始抽奖",Toast.LENGTH_SHORT).show();                break;            case R.id.person_invite:                //邀请奖励                Toast.makeText(this,"邀请奖励",Toast.LENGTH_SHORT).show();                break;            case R.id.person_set:                //设置                Toast.makeText(this,"设置",Toast.LENGTH_SHORT).show();                break;        }    }}

TranslucentScrollView.java

package com.example.personview.widget;import android.animation.ObjectAnimator;import android.animation.ValueAnimator;import android.content.Context;import android.graphics.Color;import android.support.annotation.ColorInt;import android.support.v4.graphics.ColorUtils;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.ScrollView;import com.example.personview.R;import com.example.personview.utils.SizeUtils;/** * Created by 晓辉(xiaohui) * time:2019/8/13 */public class TranslucentScrollView extends ScrollView {    static final String TAG = "TranslucentScrollView";    //伸缩视图    private View zoomView;    //伸缩视图初始高度    private int zoomViewInitHeight = 0;    // 记录首次按下位置    private float mFirstPosition = 0;    // 是否正在放大    private Boolean mScaling = false;    //渐变的视图    private View transView;    //渐变颜色    private int transColor = Color.WHITE;    //渐变开始位置    private int transStartY = 50;    //渐变结束位置    private int transEndY = 300;    //渐变开始默认位置,Y轴,50dp    private final int DFT_TRANSSTARTY = 50;    //渐变结束默认位置,Y轴,300dp    private final int DFT_TRANSENDY = 300;    private TranslucentScrollView.TranslucentChangedListener translucentChangedListener;    public interface TranslucentChangedListener {        /**         * 透明度变化,取值范围0-255         *         * @param transAlpha         */        void onTranslucentChanged(int transAlpha);    }    public TranslucentScrollView(Context context) {        super(context);    }    public TranslucentScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public TranslucentScrollView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public void setTranslucentChangedListener(TranslucentScrollView.TranslucentChangedListener translucentChangedListener) {        this.translucentChangedListener = translucentChangedListener;    }    /**     * 设置伸缩视图     *     * @param zoomView     */    public void setPullZoomView(View zoomView) {        this.zoomView = zoomView;        zoomViewInitHeight = zoomView.getLayoutParams().height;        if (zoomViewInitHeight == LayoutParams.MATCH_PARENT || zoomViewInitHeight == WindowManager.LayoutParams.WRAP_CONTENT) {            zoomView.post(new Runnable() {                @Override                public void run() {                    zoomViewInitHeight = TranslucentScrollView.this.zoomView.getHeight();                }            });        }    }    /**     * 设置渐变视图     *     * @param transView 渐变的视图     */    public void setTransView(View transView) {        setTransView(transView, getResources().getColor(R.color.colorPrimary), SizeUtils.dip2px(getContext(), DFT_TRANSSTARTY), SizeUtils.dip2px(getContext(), DFT_TRANSENDY));    }    /**     * 设置渐变视图     *     * @param transView  渐变的视图     * @param transColor 渐变颜色     * @param transEndY  渐变结束位置     */    public void setTransView(View transView, @ColorInt int transColor, int transStartY, int transEndY) {        this.transView = transView;        //初始视图-透明        this.transView.setBackgroundColor(ColorUtils.setAlphaComponent(transColor, 0));        this.transStartY = transStartY;        this.transEndY = transEndY;        this.transColor = transColor;        if (transStartY > transEndY) {            throw new IllegalArgumentException("transStartY 不得大于 transEndY .. ");        }    }    /**     * 获取透明度     *     * @return     */    private int getTransAlpha() {        float scrollY = getScrollY();        if (transStartY != 0) {            if (scrollY <= transStartY) {                return 0;            } else if (scrollY >= transEndY) {                return 255;            } else {                return (int) ((scrollY - transStartY) / (transEndY - transStartY) * 255);            }        } else {            if (scrollY >= transEndY) {                return 255;            }            return (int) ((transEndY - scrollY) / transEndY * 255);        }    }    /**     * 重置ZoomView     */    private void resetZoomView() {        final ViewGroup.LayoutParams lp = zoomView.getLayoutParams();        final float h = zoomView.getLayoutParams().height;// ZoomView当前高度        // 设置动画        ValueAnimator anim = ObjectAnimator.ofFloat(0.0F, 1.0F).setDuration(200);        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                float cVal = (Float) animation.getAnimatedValue();                lp.height = (int) (h - (h - zoomViewInitHeight) * cVal);                zoomView.setLayoutParams(lp);            }        });        anim.start();    }    @Override    protected void onScrollChanged(int l, int t, int oldl, int oldt) {        super.onScrollChanged(l, t, oldl, oldt);        int transAlpha = getTransAlpha();        if (transView != null) {            Log.d(TAG, "[onScrollChanged .. in ], 透明度 == " + transAlpha);            transView.setBackgroundColor(ColorUtils.setAlphaComponent(transColor, transAlpha));        }        if (translucentChangedListener != null) {            translucentChangedListener.onTranslucentChanged(transAlpha);        }    }    @Override    public boolean onTouchEvent(MotionEvent event) {        if (zoomView != null) {            ViewGroup.LayoutParams params = zoomView.getLayoutParams();            switch (event.getAction()) {                case MotionEvent.ACTION_UP:                    //手指离开后恢复图片                    mScaling = false;                    resetZoomView();                    break;                case MotionEvent.ACTION_MOVE:                    if (!mScaling) {                        if (getScrollY() == 0) {                            mFirstPosition = event.getY();                        } else {                            break;                        }                    }                    int distance = (int) ((event.getY() - mFirstPosition) * 0.6);                    if (distance < 0) {                        break;                    }                    mScaling = true;                    params.height = zoomViewInitHeight + distance;                    Log.d(TAG, "params.height == " + params.height + ", zoomViewInitHeight == " + zoomViewInitHeight + ", distance == " + distance);                    zoomView.setLayoutParams(params);                    return true;            }        }        return super.onTouchEvent(event);    }}

TranslucentActionBar.java

package com.example.personview.widget;import android.content.Context;import android.text.TextUtils;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.TextView;import com.example.personview.R;import com.example.personview.impl.ActionBarClickListener;/** * 支持渐变的 actionBar * Created by 晓辉(xiaohui) * time: 2019/8/13. */public final class TranslucentActionBar extends LinearLayout {    private View layRoot;    private View vStatusBar;    private View layLeft;    private View layRight;    public TextView tvTitle;    private TextView tvLeft;    private TextView tvRight;    private View iconLeft;    private View iconRight;    public TranslucentActionBar(Context context) {        this(context, null);    }    public TranslucentActionBar(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public TranslucentActionBar(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    private void init() {        setOrientation(HORIZONTAL);        View contentView = inflate(getContext(), R.layout.actionbar_trans, this);        layRoot = contentView.findViewById(R.id.lay_transroot);        vStatusBar = contentView.findViewById(R.id.v_statusbar);        tvTitle = (TextView) contentView.findViewById(R.id.tv_actionbar_title);        tvLeft = (TextView) contentView.findViewById(R.id.tv_actionbar_left);        tvRight = (TextView) contentView.findViewById(R.id.tv_actionbar_right);        iconLeft = contentView.findViewById(R.id.iv_actionbar_left);        iconRight = contentView.findViewById(R.id.v_actionbar_right);    }    /**     * 设置状态栏高度     *     * @param statusBarHeight     */    public void setStatusBarHeight(int statusBarHeight) {        ViewGroup.LayoutParams params = vStatusBar.getLayoutParams();        params.height = statusBarHeight;        vStatusBar.setLayoutParams(params);    }    /**     * 设置是否需要渐变     */    public void setNeedTranslucent() {        setNeedTranslucent(true, false);    }    /**     * 设置是否需要渐变,并且隐藏标题     *     * @param translucent     */    public void setNeedTranslucent(boolean translucent, boolean titleInitVisibile) {        if (translucent) {            layRoot.setBackgroundDrawable(null);        }        if (!titleInitVisibile) {            tvTitle.setVisibility(View.GONE);        }    }    /**     * 设置标题     *     * @param strTitle     */    public void setTitle(String strTitle) {        if (!TextUtils.isEmpty(strTitle)) {            tvTitle.setText(strTitle);        } else {            tvTitle.setVisibility(View.GONE);        }    }    /**     * 设置数据     *     * @param strTitle     * @param resIdLeft     * @param strLeft     * @param resIdRight     * @param strRight     * @param listener     */    public void setData(String strTitle, int resIdLeft, String strLeft, int resIdRight, String strRight, final ActionBarClickListener listener) {        if (!TextUtils.isEmpty(strTitle)) {            tvTitle.setText(strTitle);        } else {            tvTitle.setVisibility(View.GONE);        }        if (!TextUtils.isEmpty(strLeft)) {            tvLeft.setText(strLeft);            tvLeft.setVisibility(View.VISIBLE);        } else {            tvLeft.setVisibility(View.GONE);        }        if (!TextUtils.isEmpty(strRight)) {            tvRight.setText(strRight);            tvRight.setVisibility(View.VISIBLE);        } else {            tvRight.setVisibility(View.GONE);        }        if (resIdLeft == 0) {            iconLeft.setVisibility(View.GONE);        } else {            iconLeft.setBackgroundResource(resIdLeft);            iconLeft.setVisibility(View.VISIBLE);        }        if (resIdRight == 0) {            iconRight.setVisibility(View.GONE);        } else {            iconRight.setBackgroundResource(resIdRight);            iconRight.setVisibility(View.VISIBLE);        }        if (listener != null) {            layLeft = findViewById(R.id.lay_actionbar_left);            layRight = findViewById(R.id.lay_actionbar_right);            layLeft.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                    listener.onLeftClick();                }            });            layRight.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                    listener.onRightClick();                }            });        }    }}

布局文件:activity_main.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="match_parent"    xmlns:app="http://schemas.android.com/apk/res-auto">    <com.example.personview.widget.TranslucentScrollView        android:id="@+id/pullzoom_scrollview"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical">            <LinearLayout                android:id="@+id/lay_header"                android:layout_width="match_parent"                android:layout_height="248dp"                android:background="@mipmap/bg_banner_my"                android:orientation="vertical">                <View                    android:id="@+id/v_statusbar"                    android:layout_width="match_parent"                    android:layout_height="0.0dp"                    android:background="@color/translate" />                <RelativeLayout                    android:layout_width="match_parent"                    android:layout_height="match_parent">                    <RelativeLayout                        android:layout_width="137dp"                        android:layout_height="137dp"                        android:layout_centerInParent="true"                        android:background="@mipmap/bg_avatar">                        <de.hdodenhof.circleimageview.CircleImageView                            android:id="@+id/image_head"                            android:layout_width="105dp"                            android:layout_height="105dp"                            android:layout_centerInParent="true"                            android:src="@mipmap/dft_avatar"                            app:civ_border_color="#FFFFFF"                            app:civ_border_width="1dp" />                    </RelativeLayout>                    <LinearLayout                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_alignParentBottom="true"                        android:layout_centerHorizontal="true"                        android:layout_marginBottom="15dp"                        android:orientation="vertical">                    <LinearLayout                        android:layout_width="match_parent"                        android:layout_height="match_parent"                        android:gravity="center">                        <TextView                            android:id="@+id/name"                            style="@style/text_white"                            android:layout_gravity="center_horizontal"                            android:text="@string/name"                            android:textColor="@color/white"                            android:textSize="17sp" />                    </LinearLayout>                        <LinearLayout                            android:layout_width="match_parent"                            android:layout_height="match_parent"                            android:gravity="center">                            <TextView                                android:id="@+id/frag4_age"                                android:layout_width="30dp"                                android:layout_height="22dp"                                android:background="@drawable/bg_person_left"                                android:gravity="center_horizontal"                                android:text="20"                                android:textSize="11sp" />                            <ImageView                             android:id="@+id/frag4_sex"                             android:layout_width="30dp"                             android:layout_height="22dp"                             android:paddingRight="6dp"                             android:paddingTop="4dp"                             android:paddingBottom="2dp"                             android:background="@drawable/bg_person_right"                             android:src="@drawable/ic_person_man" />                        </LinearLayout>                    </LinearLayout>                </RelativeLayout>            </LinearLayout>            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:background="@color/white"                android:orientation="horizontal"                android:paddingTop="8dp"                android:paddingBottom="8dp">                <LinearLayout                    android:id="@+id/lay_car"                    android:layout_width="0.0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1.0"                    android:gravity="center_horizontal"                    android:orientation="vertical">                    <ImageView                        android:layout_width="50dp"                        android:layout_height="50dp"                        android:scaleType="fitXY"                        android:src="@mipmap/ic_shopcar_my" />                    <TextView                        style="@style/text_black"                        android:layout_marginTop="5dp"                        android:text="@string/contribute" />                    <TextView                        style="@style/text_black"                        android:id="@+id/contribute"                        android:layout_marginTop="5dp"                        android:text="1000+" />                </LinearLayout>                <LinearLayout                    android:id="@+id/lay_history"                    android:layout_width="0.0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1.0"                    android:gravity="center_horizontal"                    android:orientation="vertical">                    <ImageView                        android:id="@+id/imageView"                        android:layout_width="50dp"                        android:layout_height="50dp"                        android:scaleType="fitXY"                        android:src="@mipmap/ic_consume_history" />                    <TextView                        style="@style/text_black"                        android:layout_width="wrap_content"                        android:layout_marginTop="5dp"                        android:text="@string/grade" />                    <TextView                        style="@style/text_black"                        android:id="@+id/grade"                        android:layout_width="wrap_content"                        android:layout_marginTop="5dp"                        android:text="V10" />                </LinearLayout>                <LinearLayout                    android:id="@+id/lay_wallet"                    android:layout_width="0.0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1.0"                    android:gravity="center_horizontal"                    android:orientation="vertical">                    <ImageView                        android:layout_width="50dp"                        android:layout_height="50dp"                        android:scaleType="fitXY"                        android:src="@mipmap/ic_wallet" />                    <TextView                        style="@style/text_black"                        android:layout_marginTop="5dp"                        android:text="@string/coin" />                    <TextView                        style="@style/text_black"                        android:id="@+id/coin"                        android:layout_marginTop="5dp"                        android:text="500" />                </LinearLayout>            </LinearLayout>            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:background="@color/white"                android:orientation="vertical">                <View                    android:layout_width="match_parent"                    android:layout_height="15dp"                    android:background="@color/bg_main" />                <LinearLayout                    android:id="@+id/person_sign"                    android:layout_width="match_parent"                    android:layout_height="55dp"                    android:background="@color/white"                    android:orientation="horizontal">                    <ImageView                        android:layout_width="15dp"                        android:layout_height="15dp"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="16dp"                        android:background="@mipmap/ic_sign" />                    <TextView                        style="@style/text_black"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="17dp"                        android:text="@string/sign" />                    <TextView                        android:id="@+id/tv_has_sign"                        style="@style/text_black"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="3dp"                        android:layout_weight="1"                        android:text="@string/sign"                        android:textColor="@color/gray_dft"                        android:textSize="13sp"                        android:visibility="invisible" />                    <ImageView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginRight="16dp"                        android:background="@mipmap/ic_right_gray" />                </LinearLayout>                <View                    style="@style/horizontal_divider_light"                    android:layout_marginLeft="16dp" />                <LinearLayout                    android:id="@+id/person_footprint"                    android:layout_width="match_parent"                    android:layout_height="55dp"                    android:background="@color/white"                    android:orientation="horizontal">                    <ImageView                        android:layout_width="15dp"                        android:layout_height="15dp"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="16dp"                        android:background="@mipmap/ic_address" />                    <TextView                        style="@style/text_black"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="17dp"                        android:layout_weight="1"                        android:text="@string/footprint" />                    <ImageView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginRight="16dp"                        android:background="@mipmap/ic_right_gray" />                </LinearLayout>                <View                    android:layout_width="match_parent"                    android:layout_height="15dp"                    android:background="@color/bg_main" />                <LinearLayout                    android:id="@+id/person_publish"                    android:layout_width="match_parent"                    android:layout_height="55dp"                    android:background="@color/white"                    android:orientation="horizontal">                    <ImageView                        android:layout_width="15dp"                        android:layout_height="15dp"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="16dp"                        android:background="@mipmap/ic_agent_my" />                    <TextView                        style="@style/text_black"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="17dp"                        android:layout_weight="1"                        android:text="@string/publish" />                    <ImageView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginRight="16dp"                        android:background="@mipmap/ic_right_gray" />                </LinearLayout>                <View                    style="@style/horizontal_divider_light"                    android:layout_marginLeft="16dp" />                <LinearLayout                    android:id="@+id/person_photowall"                    android:layout_width="match_parent"                    android:layout_height="55dp"                    android:background="@color/white"                    android:orientation="horizontal">                    <ImageView                        android:layout_width="15dp"                        android:layout_height="15dp"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="16dp"                        android:background="@mipmap/ic_teacher_my" />                    <TextView                        style="@style/text_black"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="17dp"                        android:layout_weight="1"                        android:text="@string/photowall" />                    <ImageView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginRight="16dp"                        android:background="@mipmap/ic_right_gray" />                </LinearLayout>                <View                    android:layout_width="match_parent"                    android:layout_height="15dp"                    android:background="@color/bg_main" />                <LinearLayout                    android:id="@+id/person_lucky"                    android:layout_width="match_parent"                    android:layout_height="55dp"                    android:background="@color/white"                    android:orientation="horizontal">                    <ImageView                        android:layout_width="15dp"                        android:layout_height="15dp"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="16dp"                        android:background="@mipmap/ic_luck_my" />                    <TextView                        style="@style/text_black"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="17dp"                        android:layout_weight="1"                        android:text="@string/lucky" />                    <ImageView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginRight="16dp"                        android:background="@mipmap/ic_right_gray" />                </LinearLayout>                <View                    style="@style/horizontal_divider_light"                    android:layout_marginLeft="16dp" />                <LinearLayout                    android:id="@+id/person_invite"                    android:layout_width="match_parent"                    android:layout_height="55dp"                    android:background="@color/white"                    android:orientation="horizontal">                    <ImageView                        android:layout_width="15dp"                        android:layout_height="15dp"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="16dp"                        android:background="@mipmap/ic_invite_my" />                    <TextView                        style="@style/text_black"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="17dp"                        android:layout_weight="1"                        android:text="@string/invite" />                    <ImageView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginRight="16dp"                        android:background="@mipmap/ic_right_gray" />                </LinearLayout>                <View                    android:layout_width="match_parent"                    android:layout_height="15dp"                    android:background="@color/bg_main" />                <LinearLayout                    android:id="@+id/person_set"                    android:layout_width="match_parent"                    android:layout_height="55dp"                    android:background="@color/white"                    android:orientation="horizontal">                    <ImageView                        android:layout_width="15dp"                        android:layout_height="15dp"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="16dp"                        android:background="@mipmap/ic_set_my" />                    <TextView                        style="@style/text_black"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginLeft="17dp"                        android:layout_weight="1"                        android:text="@string/set" />                    <ImageView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center_vertical"                        android:layout_marginRight="16dp"                        android:background="@mipmap/ic_right_gray" />                </LinearLayout>                <View                    android:layout_width="match_parent"                    android:layout_height="140dp"                    android:background="@color/bg_main" />            </LinearLayout>        </LinearLayout>    </com.example.personview.widget.TranslucentScrollView>    <com.example.personview.widget.TranslucentActionBar        android:id="@+id/actionbar"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></RelativeLayout>

activity_actionbar.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <com.example.personview.widget.TranslucentActionBar        android:id="@+id/actionbar"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <com.example.personview.widget.TranslucentScrollView        android:id="@+id/scrollview"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="1000dp"            android:orientation="vertical" />    </com.example.personview.widget.TranslucentScrollView></LinearLayout>

actionbar_trans.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/lay_transroot"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@color/colorPrimary"    android:orientation="vertical">    <View        android:id="@+id/v_statusbar"        android:layout_width="match_parent"        android:layout_height="1.0dp" />    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="45dp"        android:orientation="vertical">        <RelativeLayout            android:id="@+id/lay_actionbar_left"            android:layout_width="100dp"            android:layout_height="match_parent"            android:orientation="horizontal">            <ImageView                android:id="@+id/iv_actionbar_left"                android:layout_width="20dp"                android:layout_height="20dp"                android:layout_centerVertical="true"                android:layout_marginLeft="10dp"                android:background="@mipmap/ic_left_light"                android:visibility="gone" />            <TextView                android:id="@+id/tv_actionbar_left"                style="@style/text_white"                android:layout_height="match_parent"                android:layout_marginLeft="10dp"                android:layout_toRightOf="@+id/iv_actionbar_left"                android:gravity="center_vertical"                android:maxLength="2"                android:singleLine="true"                android:text="返回"                android:visibility="gone" />        </RelativeLayout>        <TextView            android:id="@+id/tv_actionbar_title"            style="@style/text_white"            android:layout_centerInParent="true"            android:text="标题"            android:textSize="16sp" />        <RelativeLayout            android:id="@+id/lay_actionbar_right"            android:layout_width="100dp"            android:layout_height="match_parent"            android:layout_alignParentRight="true"            android:gravity="right"            android:orientation="horizontal">            <View                android:id="@+id/v_actionbar_right"                android:layout_width="20dp"                android:layout_height="20dp"                android:layout_alignParentRight="true"                android:layout_centerVertical="true"                android:layout_marginRight="10dp"                android:visibility="gone" />            <TextView                android:id="@+id/tv_actionbar_right"                style="@style/text_white"                android:layout_height="match_parent"                android:layout_marginRight="10dp"                android:layout_toLeftOf="@+id/v_actionbar_right"                android:gravity="center_vertical|right"                android:singleLine="true"                android:visibility="gone" />        </RelativeLayout>    </RelativeLayout></LinearLayout>

注意:全面屏要使标题栏渐变,除了需要在mainActivity.java里声明外,还要在AndroidManifest.xml中将theme设置成为android:theme="@style/TranslucentTheme";并且引入三个style样式文件,适应不同的机型。

好了核心代码就是这些,如下有项目完整包

下载地址:https://download.csdn.net/download/qq_41894451/11538983

更多相关文章

  1. android EditText设置不可写
  2. android“设置”里的版本号
  3. 在Fragment中设置控件点击方法,执行失败。
  4. android中文api(89)——ViewManager
  5. Android(安卓)闹钟管理类的使用
  6. Android中的FILL_PARENT与WRAP_CONTENT的区别
  7. Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色
  8. android 设置中划线 下划线等
  9. Andorid Dialog 示例【慢慢更新】

随机推荐

  1. 【android 其他】转载:Android 简史
  2. Android LCD(四):LCD驱动调试篇
  3. android的对话框
  4. Android:EditText 多行显示、属性汇总、族
  5. Android activity属性设置大全
  6. 其实android也可以使用jetty的comet客户
  7. Android EditView属性
  8. ListView之setEmptyView的问题
  9. ActionBar
  10. Android(安卓)UmengShareSDK第三方登录