0

我的应用程序中有 3 项活动,主要一项和其他 2 项。在主要的内部,我在 onCreate 方法中使用了这段代码来使应用程序全屏运行:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    this.findViewById(R.id.main_screen).setOnTouchListener(this);

    width = Util.getInstance().getScreenWidth(this);
    height = Util.getInstance().getScreenHeight(this);

    String imagePath = PreferenceManager.getDefaultSharedPreferences(this).getString("IMAGE_PATH", "NULL");
    if (!"NULL".equals(imagePath) && new File(imagePath).exists()){
        Util.IMAGE_PATH = imagePath;
        setImage();
    }
}

我在其他 2 个活动中使用了相同的代码,如下所示,但它们似乎没有任何效果。它仍然不像主要的那样全屏显示它。以下是另外两个活动:

public class Pass extends Activity implements OnClickListener, OnKeyListener, OnTouchListener{


EditText etPassword;
    public static StringBuilder pass;
    public static String replace;
    static boolean keyDel = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pass);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        etPassword = (EditText) findViewById(R.id.password);

        final StringBuilder stringBuilderHiddenPassword = new StringBuilder();
        final StringBuilder mPass = new StringBuilder();
        pass = new StringBuilder();
        etPassword.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_DEL){
                    keyDel = true;
                }else{
                    keyDel = false;
                }
                return false;
            }
        });

        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!keyDel){
                    stringBuilderHiddenPassword.append("*");
                    etPassword.removeTextChangedListener(this);
                    etPassword.setText(stringBuilderHiddenPassword);
                    etPassword.addTextChangedListener(this);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                pass.append(s);
                replace = pass.toString().replace("*", "");
            }
        });

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        this.findViewById(R.id.pass_screen).setBackgroundColor(Color.argb(100, 0, 0, 0));

        this.findViewById(R.id.pass_cancel).setOnClickListener(this);
        this.findViewById(R.id.pass_ok).setOnClickListener(this);
        this.findViewById(R.id.password).setOnKeyListener(this);
        this.findViewById(R.id.pass_screen).setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.pass_screen) {
            this.finish();
            return true;
        }

        else return false;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case(R.id.pass_cancel): this.finish(); break;
        case(R.id.pass_ok): checkPass(); break;
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() != MotionEvent.ACTION_UP)
            return false;

        switch(keyCode){
        case(KeyEvent.KEYCODE_ENTER):
        case(KeyEvent.KEYCODE_DPAD_CENTER): checkPass(); break;
        default: return false;
        }



return true;
}

private void checkPass(){
    if (replace.equals(Util.PASS)){
        showMenu(this);
    }
    else {
        ((EditText)this.findViewById(R.id.password)).setText("");
        Util.getInstance().error("Wrong Password!", this);
    }
}

private void showMenu(final Activity act){
    Thread t = new Thread(){
        public void run(){
            act.finish();
            act.startActivity(new Intent(act, Menu.class));
        }
    };
    t.start();
}
}

和另一个:

public class Menu extends Activity implements OnItemClickListener {
    private GridView thumbs;
    private ImageAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        adapter = new ImageAdapter(getApplicationContext());

        thumbs = (GridView)this.findViewById(R.id.thumbs);
        thumbs.setAdapter(adapter);
        thumbs.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        ((Layout)v).setFocus(true);
        setImage(adapter.getItem(position));
    }

    private void setImage(String pic){
        Util.IMAGE_PATH = pic;
        Util.SET_IMAGE_FLAG = true;

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.edit().putString("IMAGE_PATH", Util.IMAGE_PATH).commit();

        this.finish();
    }
}

由于不熟悉沉浸式模式,我希望你们中的一些人可以提供答案。

4

2 回答 2

1

在您的 Manifest.xml 文件中,在活动标记中添加属性

<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
于 2017-07-19T03:36:16.737 回答
0

setSystemUiVisibility例如,您可以从 Activity 创建简单的扩展类并将其用作父类,而不是在所有活动中使用

家长:

public class BaseActivity extends Activity {
    private GridView thumbs;
    private ImageAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    }
}

孩子:

public class Pass extends BaseActivity implements OnClickListener, OnKeyListener, OnTouchListener{

...

}

更新1:测试这个解决方案

创建自定义样式

<style name="Theme.myFullScreen" parent="android:Theme">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

在清单上设置:

android:theme="@style/Theme.myFullScreen"
于 2017-07-18T20:19:24.470 回答