Android 의 몰입 식 상태 표시 줄 의 실현 방법,상태 표시 줄 투명
10109 단어 android몰입 식 상태 표시 줄
주의!몰입 형 상태 표시 줄 은 안 드 로 이 드 4.4 이상 버 전 만 지원 합 니 다.
상태 표시 줄:4.4 는 그 라 데 이 션 색 이 고 5.0 은 완전히 투명 하 며 본 고 는 시 뮬 레이 터 가 4.4 로 보 여 줍 니 다.
효과 그림:
주의!두 가지 방법의 차이:
첫 번 째:상단 표시 줄 에 현재 activity 의 레이아웃 파일 의 배경 색 을 따라 사용 하기에 편리 합 니 다.그러나 문 제 는 아래쪽 가상 네 비게 이 션 키 가 있 으 면 네 비게 이 션 키 의 배경 이 상단 의 색 과 같 습 니 다.예 를 들 어:
두 번 째:상단 표시 줄 의 색상 을 설정 하여 표시 합 니 다.첫 번 째 부족 한 점 을 해결 할 수 있 습 니 다.예 를 들 어:
첫 번 째 사용 방법:
첫째,먼저 values,values-v19,values-v21 폴 더 의 styles.xml 에 Translucent System Bar 스타일 의 Theme 를 설정 합 니 다.다음 그림 과 같 습 니 다.
values/style.xml:
<style name="TranslucentTheme" parent="AppTheme">
<!-- Android 4.4 , -->
</style>
values-v19/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
values-v21/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x , -->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
둘째,목록 파일 에 몰입 식 상태 표시 줄 이 필요 한 activity 를 설정 하여 theme 에 추가 합 니 다.
<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" />
<activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />
셋째,Activity 의 레이아웃 파일 에'android:fitsSystem Windows='true'를 추가 합 니 다.하지만 배경 이 그림 인지 단색 인지 구분 해 야 합 니 다.1.배경 이 그림 일 때 레이아웃 은 이렇게 쓸 수 있 습 니 다.
<?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"
android:background="@drawable/imgs_bj"
android:fitsSystemWindows="true">
</RelativeLayout>
효과:2.배경 이 순수한 색 이면 우 리 는 레이아웃 을 구분 해 야 한다.제목 레이아웃 과 내용 레이아웃 은 먼저 루트 레이아웃 배경 을 제목 레이아웃 의 배경 색 으로 설정 한 다음 에 제목 배경 색 은 루트 레이아웃 을 직접 사용 하 는 배경 색 을 설정 하지 않 고 마지막 으로 내용 레이아웃 배경 색 을 흰색 으로 설정 할 수 있다.
<?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:background="@color/colorPrimary" // “ ”
android:fitsSystemWindows="true"
android:orientation="vertical">
<!-- -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/color_31c27c">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text=" "
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
<!-- -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" //
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_marginTop="120dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text=" "
android:onClick="showMsg"
/>
</LinearLayout>
</LinearLayout>
효과 그림:자,이상 은 몰입 식 상태 표시 줄 이 실현 되 는 전 과정 입 니 다.그러나 주의해 야 할 것 은 우리 가 activity 가 비교적 많 으 면 모든 페이지 에 Android:fitsSystem Windows="true"를 추가 하 는 것 이 귀 찮 습 니 다.우 리 는 변경 해 야 합 니 다.
기본 클래스 BaseColorActivity.class 를 쓰 십시오.코드 는 다음 과 같 습 니 다.
public abstract class BaseColorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ! !!!!
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(getLayoutResId());//
ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
View parentView = contentFrameLayout.getChildAt(0);
if (parentView != null && Build.VERSION.SDK_INT >= 14) {
parentView.setFitsSystemWindows(true);
}
}
/**
* Activity id
*
* @return
*/
abstract protected int getLayoutResId();
}
그리고 몰입 상태 표시 줄 의 activity 가 이 기본 클래스 를 계승 해 야 합 니 다:
public class ColorActivity extends BaseColorActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView()!
}
@Override
protected int getLayoutResId() {
//onCreate setContentView(), activity !
return R.layout.activity_color;
}
}
그리고 상태 표시 줄 에 몰입 해 야 하 는 activity 의 레이아웃 파일 에서 android:fitsSystem Windows="true"코드 를 생략 할 수 있 습 니 다!두 번 째 사용 방법(미 완성):
도구 클래스 Status BarCompat.class 를 작성 합 니 다:
public class StatusBarCompat {
private static final int INVALID_VAL = -1;
private static final int COLOR_DEFAULT = Color.parseColor("#20000000");
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void compat(Activity activity, int statusColor)
{
// 5.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
if (statusColor != INVALID_VAL)
{
activity.getWindow().setStatusBarColor(statusColor);
}
return;
}
// 4.4
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
{
int color = COLOR_DEFAULT;
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
if (statusColor != INVALID_VAL)
{
color = statusColor;
}
View statusBarView = new View(activity);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight(activity));
statusBarView.setBackgroundColor(color);
contentView.addView(statusBarView, lp);
}
}
public static void compat(Activity activity)
{
compat(activity, INVALID_VAL);
}
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;
}
}
사용 방법:현재 activity 의 onCreate 에서 호출 방법 StatusBarCompat.compat 를 사용 하면 됩 니 다.
//
StatusBarCompat.compat(this, Color.RED);
모든 activity 가 귀 찮 게 쓰 여야 한다 면 기본 클래스 를 써 서 이 단 계 를 완성 하 세 요.
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
StatusBarCompat.compat(this, Color.RED);
}
}
그리고 모든 activity 페이지 에서 이 BaseActivity 를 계승 하면 됩 니 다!위의 코드 에서 주 의 를 제시 한 곳 에 대한 설명:
시스템 title 에서 주의 할 점 숨 기기:
1、AppCompatActivity 계승 시 사용:
supportRequestWindowFeature(Window.FEATURENOTITLE)
2、activity 계승 시 사용:
requestWindowFeature(Window.FEATURENOTITLE)
텍스트 관련 다운로드:클릭 하여 원본 및 apk 파일 무료 다운로드이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.