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 파일 무료 다운로드
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기