【问题标题】:How to make the actionbar disappear in Android Studio?如何使操作栏在 Android Studio 中消失?
【发布时间】:2020-01-05 20:58:51
【问题描述】:

我需要让操作栏从屏幕上消失,我已经尝试了所有可以在网上找到的方法,但似乎没有任何效果。我尝试创建自己的样式,但它什么都不做,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】:

清单

<?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
               );
}

【讨论】:

  • 我不完全确定该代码应该去哪里,这是我的原始代码:
  • 在 super.onCreate(savedInstanceState);在 setContentView(R.layout.activity_main); 之前就像在我的帖子回答中一样
  • super.onCreate(savedInstanceState);覆盖乐趣 onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE) window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) setContentView(R.layout.activity_picture_viewer) } setContentView(R .layout.activity_main);
  • 喜欢这个?
  • 是的,就是这样,试着告诉我是否工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
相关资源
最近更新 更多