如何使操作栏在Android Studio中消失?

问题描述 投票:3回答:1

我需要使操作栏从屏幕上消失,我已经尝试了所有可以在网上找到的内容,但似乎没有任何效果。我尝试创建自己的样式,它什么都不做,getActionbar().hide(),它给我一个错误,并且不能编译,以及requestWindowFeature(Window.FEATURE_NO_TITLE);,它也什么都不做。我真的需要摆脱那个行动吧。我正在使用sensorLandscape方向,但是我已经尝试过对其进行更改,并且它什么都不会更改。

清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tanques">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen>
        <activity android:name=".MainActivity" android:screenOrientation="sensorLandscape" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>

我自己的风格:

<resources>

    <!-- Base application theme. -->

    <style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
</resources>

MainActivity.java

package com.example.tanques;

import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements JoystickView.JoystickListener, JoystickHorizontal.JoystickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button ShootLeft= findViewById(R.id.ShootLeft);
        Button ShootRight= findViewById(R.id.ShootRight);
        ShootLeft.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){
            Log.d("Button","ShootLeft");
        }});
        ShootRight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){
            Log.d("Button","ShootRight");
        }});
    }

    @Override
    public void onJoystickMoved(float Percent, int source) {
        switch(source){
            case R.id.JoystickLeft:
                Log.d("Joystick", "JoystickLeft" + " Percentage: " +Math.round(Percent));
                break;
            case R.id.JoystickRight:
                Log.d("Joystick", "JoystickRight" + " Percentage: " +Math.round(Percent));
                break;
            case R.id.JoystickTurret:
                Log.d("Joystick", "JoystickTurret" + " Percentage: " +Math.round(Percent));
                break;
        }
    }
}
java android xml android-layout
1个回答
1
投票

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tanques">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>

styles.xml

<resources>

    <!-- Base application theme. -->

    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"/>

</resources>

将其放在onCreate()之前的MainActivity.java中的setContentView()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //add this two lines to your code to hide the system bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    //this hides the navigation bar at the bottom of your device
    hideNavigationBar();

    final Button ShootLeft= findViewById(R.id.ShootLeft);
    Button ShootRight= findViewById(R.id.ShootRight);
    ShootLeft.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){
        Log.d("Button","ShootLeft");
    }});
    ShootRight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){
        Log.d("Button","ShootRight");
    }});
}

创建hideNavigationBar()方法并在onCreate()中调用它

private void hideNavigationBar(){
    getWindow().getDecorView()
               .setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
               );
}
© www.soinside.com 2019 - 2024. All rights reserved.