如何从android活动中删除标题栏?

vim*_*hra 28 android android-layout

有人可以帮我解决这个问题.我希望我的活动全屏,并希望从屏幕上删除标题.我尝试了几种方法,但无法将其删除.

活动代码:

public class welcomepage extends Activity {
    private Button btn;
    EditText userName,passWord;
    DatabaseHandler dbHandler;
    Context ctx =this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_welcomepage);
   }
}
Run Code Online (Sandbox Code Playgroud)

和Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout    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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/pic1"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.edkul.vimal.edkul.welcomepage">

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我想删除以蓝色显示的标题栏.请找到参考图像:

在此输入图像描述

AndroidManifest.xml中

<application
        android:minSdkVersion="3"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity
            android:name=".welcomepage"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

Sha*_*esh 93

您只需style.xml在您的值文件夹中的文件中添加此样式即可

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

之后,将此样式设置为AndroidManifest.xml文件中的活动类

android:theme="@style/AppTheme.NoActionBar"
Run Code Online (Sandbox Code Playgroud)

编辑: -如果你想按程序方式隐藏ActionBar,那么在你的activity onCreate()方法中使用下面的代码.

if(getSupportedActionbar()!=null)    
     this.getSupportedActionBar().hide();
Run Code Online (Sandbox Code Playgroud)

如果你想从Fragment隐藏动作栏,那么也是可能的

getActivity().getSupportedActionBar().hide();
Run Code Online (Sandbox Code Playgroud)

程序兼容性V7: -在活动中的运用以下主题,你不想actiobBar Theme.AppComat.NoActionBar或者Theme.AppCompat.Light.NoActionBar或者如果你想在整个应用程序隐藏然后设置这个主题在你<application... />在你的AndroidManifest.

我希望这会对你有所帮助.

  • 在 Kotlin 中,这个条目就足够了 `supportActionBar?.hide()` (3认同)

Chr*_*ter 19

试试这个:

this.getSupportActionBar()隐藏();

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

    try
    {
        this.getSupportActionBar().hide();
    }
    catch (NullPointerException e){}

    setContentView(R.layout.activity_main);
}
Run Code Online (Sandbox Code Playgroud)

  • 检查指针比捕获异常更好。 (2认同)

fs_*_*_dm 7

你可以试试:

<activity android:name=".YourActivityName"
          android:theme="@style/Theme.Design.NoActionBar">
Run Code Online (Sandbox Code Playgroud)

这对我行得通


tim*_*687 6

在您的 Android 清单文件中,请确保活动正在使用此(或)主题(基于)。@style/Theme.AppCompat.NoActionBar 这会完全删除 ActionBar,但不会使您的活动全屏显示。如果你想让你的活动全屏只使用这个主题

@android:style/Theme.NoTitleBar.Fullscreen
Run Code Online (Sandbox Code Playgroud)

或者你可以改变

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)

requestWindowFeature(Window.FEATURE_NO_TITLE);
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
Run Code Online (Sandbox Code Playgroud)

这是我用来在运行时全屏显示的

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                mDecorView = getWindow().getDecorView();
                mDecorView.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 // hide nav bar
                                | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                                | View.SYSTEM_UI_FLAG_IMMERSIVE);
            }
Run Code Online (Sandbox Code Playgroud)

要退出全屏,我使用这个

mDecorView = getWindow().getDecorView();
mDecorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
Run Code Online (Sandbox Code Playgroud)


Sou*_*Das 5

添加活动

requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)

style.xml并使用以下两行添加您的文件:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
Run Code Online (Sandbox Code Playgroud)