android 沉浸式标题栏
程序开发
2023-09-02 20:13:11
1、沉浸式标题栏,中心思想就是,自定义的状态栏打通原始系统的,并且增加一个标题栏,如图所示。
2、以上效果分为2部分,第一部分就是和状态栏完全同尺寸的自定义状态栏,第二部分就是和自定义背景颜色一样的标题栏。
3、具体代码如下。
public class BaseActivity extends AppCompatActivity implements View.OnClickListener{protected int mContentViewId;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(mContentViewId);View container = findViewById(R.id.action_bar_container);if (null != container)container.setVisibility(View.GONE);//透明状态栏if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){Window window = getWindow();window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);}int statusHeight = getStatusBarHeight(this);LinearLayout statusLinearLayout = (LinearLayout) findViewById(R.id.phone_title_status);if (null != statusLinearLayout) {ViewGroup.MarginLayoutParams params = null;if (statusLinearLayout.getLayoutParams() instanceof LinearLayout.LayoutParams){params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, statusHeight);}else if (statusLinearLayout.getLayoutParams() instanceof RelativeLayout.LayoutParams){params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, statusHeight);}if (null != params) {statusLinearLayout.setLayoutParams(params);statusLinearLayout.requestLayout();}}}public static int getStatusBarHeight(Context context) {int result = 0;int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");if (resourceId > 0) {result = context.getResources().getDimensionPixelSize(resourceId);}return result;}
}
4、phone_title_status 即是自定义状态栏,TitleBarPublic为标题栏。通过以上代码可以发现,首先获取系统状态栏的尺寸,然后将自定义的状态栏phone_title_status 的尺寸设置为系统状态栏的尺寸。
5、以下代码就是打通状态栏的代码。
//透明状态栏if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){Window window = getWindow();window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);}
以下附上自定义标题栏的代码:
package com.uuch.android_zxinglibrary.customwidgets;import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;import com.uuch.android_zxinglibrary.R;/*** Created by wangqiubo on 2018-01-16.*/public class TitleBarPublic extends TitleBarBase implements View.OnClickListener{protected HomeDropBox mHomeDropBox;protected View mShowMoreIcon = null;protected View backView = null;public TitleBarPublic(@NonNull Context context) {super(context, R.layout.phone_drop_down_title_bar);init();}public TitleBarPublic(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs, R.layout.phone_drop_down_title_bar);init();}public TitleBarPublic(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr, R.layout.phone_drop_down_title_bar);init();}@Overrideprotected void init(){super.init();mHomeDropBox= new HomeDropBox(this.getContext());mShowMoreIcon = findViewById(R.id.show_more);mShowMoreIcon.setOnClickListener(this);backView = findViewById(R.id.back_pre_page);backView.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.show_more){mHomeDropBox.showDropBox(mShowMoreIcon);}}public void configureChildControl(int type, int visible){
// mHomeDropBox.configureChildControl(type, visible);}
}
phone_drop_down_title_bar.xml
标签:
上一篇:
如何设置jupyter notebook的代码自动补全
下一篇:
两个HTML页面之间如何传值
相关文章
-
无相关信息