一手遮天 Android - Activity: Activity 样式(隐藏状态栏;改变状态栏颜色;对话框样式的 activity)
一手遮天 Android - Activity: Activity 样式(隐藏状态栏;改变状态栏颜色;对话框样式的 activity)
示例如下:
/activity/ActivityDemo6.java
/**
* Activity 样式(隐藏状态栏,改变状态栏颜色,对话框样式的 activity)
*
* 本例演示如何隐藏 activity 的 statusBar,以及如何改变 statusBar 的颜色
* 关于对话框样式的 activity 请参见 activity/ActivityDemo6_2
*/
package com.webabcd.androiddemo.activity;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import com.webabcd.androiddemo.R;
public class ActivityDemo6 extends AppCompatActivity {
private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
private Button mButton5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_activitydemo6);
mButton1 = findViewById(R.id.button1);
mButton2 = findViewById(R.id.button2);
mButton3 = findViewById(R.id.button3);
mButton4 = findViewById(R.id.button4);
mButton5 = findViewById(R.id.button5);
sample();
}
private void sample() {
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 打开对话框样式的 activity(activity/ActivityDemo6_2)
Intent intent = new Intent(ActivityDemo6.this, ActivityDemo6_2.class);
startActivity(intent);
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 隐藏 statusBar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
});
mButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 显示 statusBar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
});
mButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 改变 statusBar 的背景色
getWindow().setStatusBarColor(getResources().getColor(R.color.green));
}
});
mButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 改变 statusBar 的前景色
// 这个只有 2 种颜色,要么黑要么白
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
});
}
}
\activity\ActivityDemo6_2.java
/**
* 本例用于演示如何显示对话框样式的 activity
*
* 如果需要显示对话框样式的 activity,则需要在 AndroidManifest.xml 中设置 activity 节点的 theme 属性为 @style/Theme.AppCompat.Dialog
* 注:经测试,如果是在 java 的 setTheme() 中设置主题的话,背景只能是黑色的,无法设置为透明的
*
* 对话框样式的 activity 是有标题栏的,可以通过自定义主题将其去掉,参见 res/values/styles.xml 中的“MyActivityDialog”
*/
package com.webabcd.androiddemo.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.webabcd.androiddemo.R;
import com.webabcd.androiddemo.utils.Helper;
public class ActivityDemo6_2 extends AppCompatActivity {
private Button mButton1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_activitydemo6_2);
// 设置当前 activity 的宽和高
android.view.WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.width = (int)(Helper.getScreenSize(this).x * 0.8);
layoutParams.height = (int)(Helper.getScreenSize(this).y * 0.8);
getWindow().setAttributes(layoutParams);
// setFinishOnTouchOutside(boolean finish)
// true - 点击空白区域(非此 activity 区域)自动关闭此 activity,默认值
// false - 点击空白区域(非此 activity 区域)不会自动关闭此 activity
setFinishOnTouchOutside(false);
mButton1 = findViewById(R.id.button1);
sample();
}
private void sample() {
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
/layout/activity_activity_activitydemo6.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"
android:background="@color/red">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:text="打开对话框样式的 activity"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:text="隐藏 statusBar"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:text="显示 statusBar"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:text="改变 statusBar 的背景色"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:text="改变 statusBar 的前景色"/>
</LinearLayout>
/layout/activity_activity_activitydemo6_2.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"
android:background="@color/green">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:text="关闭"/>
</LinearLayout>