在此示例中,我们将说明如何隐藏标题栏以及如何以全屏模式显示内容。
必须调用Activity的requestWindowFeature(Window.FEATURE_NO_TITLE)方法来隐藏标题。但是,必须在setContentView方法之前对其进行编码。
隐藏活动标题栏的代码
getSupportActionBar()方法用于检索ActionBar类的实例。调用ActionBar类的hide()方法将隐藏标题栏。
requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title
getSupportActionBar().hide(); //hide the title bar
启用全屏活动模式的代码
Window类的setFlags()方法用于以全屏模式显示内容。你需要在setFlags方法中传递WindowManager.LayoutParams.FLAG_FULLSCREEN常量。
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //show the activity in full screen
Android隐藏标题栏和全屏示例
让我们看一下完整的代码,以在android中隐藏标题栏。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="first.srcmini02.com.hidetitlebar.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
活动课
package first.srcmini02.com.hidetitlebar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
setContentView(R.layout.activity_main);
}
}