Android:以编程方式使全屏显示

时间:2019-02-07 19:01:31

标签: android android-studio android-layout android-intent android-activity

我想在单击按钮时全屏显示,并在按返回按钮时恢复到正常显示。

在Android Studio中

2 个答案:

答案 0 :(得分:0)

在OnCreate()

 Button x = findViewById(R.id.button);
       x.setOnClickListener(new View.OnClickListener() {
          @Override public void onClick(View v) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
             WindowManager.LayoutParams.FLAG_FULLSCREEN);
             setContentView(R.layout.mainActivity);
          }
       });

现在您需要退出全屏模式,并使用以下代码:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); ActivitiesCurrentContentView.requestLayout();

答案 1 :(得分:-1)

您可以通过编程方式完成

    public class ActivityName extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // remove title
    Button x = findViewById(R.id.yourButton);
       x.setOnClickListener(new View.OnClickListener() {
          @Override public void onClick(View v) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
             WindowManager.LayoutParams.FLAG_FULLSCREEN);
             setContentView(R.layout.main);
          }
       });
}
}