Android 在片段活动中隐藏操作栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20135315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:21:08  来源:igfitidea点击:

Hide Action Bar in a Fragment Activity

androidandroid-fragmentactivityandroid-actionbar-compatandroid-actionbaractivity

提问by Daniel Wilson

I am creating the framework for an app and wish to hide the action bar for the login activity. The app is going to be entirely composed of fragments because they are awesome.

我正在为应用程序创建框架并希望隐藏登录活动的操作栏。该应用程序将完全由片段组成,因为它们很棒。

I can hide the action bar fine for my activity, but every time I run the app I see my inflated blank fragment and default action bar for about 2 seconds before it manages to inflate the login fragment.

我可以为我的活动很好地隐藏操作栏,但是每次我运行应用程序时,我都会看到我膨胀的空白片段和默认操作栏大约 2 秒钟,然后它才设法膨胀登录片段。

Any idea on how / where I should hide the action bar, (whilst keeping my framework) to stop this from happening?

关于我应该如何/在哪里隐藏操作栏(同时保留我的框架)以阻止这种情况发生的任何想法?

LoginActivity.java

登录活动.java

public class LoginActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
    return new LoginFragment();
}

}

}

SingleFragmentActivity.java

SingleFragmentActivity.java

public abstract class SingleFragmentActivity extends ActionBarActivity {
protected abstract Fragment createFragment();

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

    if(this instanceof LoginActivity){
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }
    setContentView(R.layout.activity_fragment);

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

    if (fragment == null) {
        fragment = createFragment();
        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragment)
                .commit();
    }
}

}

}

LoginFragment.java

登录片段.java

public class LoginFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_login, parent, false);

    return v;
}

}

}

I originally had SingleFragmentActivity extend FragmentActivity and did this in the if statement:

我最初让 SingleFragmentActivity 扩展 FragmentActivity 并在 if 语句中执行此操作:

    requestWindowFeature(Window.FEATURE_NO_TITLE);

But I've got a feeling that's just a different way of doing the same thing

但我有一种感觉,这只是做同样事情的不同方式

Edit:

编辑:

I have also tried setting the fragment_activity to use a different style but it doesn't seem to work either:

我还尝试将 fragment_activity 设置为使用不同的样式,但它似乎也不起作用:

    <style name="NoActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
</style>

回答by Daniel Wilson

Okay after a few hours I think I have found the solution to keeping fragments in place for everything and removing the action bar (for all devices) for the launch activity only:

好的,几个小时后,我想我已经找到了解决方案,可以将所有片段保留在适当的位置,并仅为启动活动移除操作栏(对于所有设备):

Android Manifest:

安卓清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.app"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name="com.company.app.LoginActivity"
        android:theme="@style/NoActionBar"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Where the style is defined as like so:

样式定义如下:

   <style name="NoActionBar" parent="Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
</style>

That took a while to figure out because there is no Theme.AppCompat.Light.NoActionBar , the action bar is basically known as the title bar in older APIs, and when I applied that style to the activity itself (which I thought was the only way of using dynamic themes) it doesn't seem to work at all.

这花了一段时间才弄清楚,因为没有 Theme.AppCompat.Light.NoActionBar ,操作栏在旧 API 中基本上被称为标题栏,当我将该样式应用于活动本身时(我认为这是唯一的使用动态主题的方式)它似乎根本不起作用。

So hopefully this simple solution will keep the action bar for any other activities and it may help someone in the future :)

因此,希望这个简单的解决方案可以保留任何其他活动的操作栏,并且将来可能会对某人有所帮助:)

回答by The Badak

try this:

尝试这个:

if(this instanceof LoginActivity) {
    if (getActionBar().isShowing()) getActionBar().hide();
} else {
    if (getActionBar().isShowing()) getActionBar().hide();
}

or

或者

if(this instanceof LoginActivity){
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
} else {
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
}

回答by pixel

Daniel Wilson's comment above does work but just a minor difference in how I did it. I did not define style at all, all I did in AndroidManifext.xml is that I added this to my activity:

Daniel Wilson 上面的评论确实有效,但与我的做法略有不同。我根本没有定义样式,我在 AndroidManifext.xml 中所做的就是将它添加到我的活动中:

android:theme="@style/Theme.AppCompat.NoActionBar"

So, I did not have to redefine style for NoActionBar, I just added it from Theme.AppCompat

所以,我不必为 NoActionBar 重新定义样式,我只是从 Theme.AppCompat 添加它

回答by Girts Strazdins

I'm using this code in Activity.onCreate()

我在 Activity.onCreate() 中使用此代码

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

    // Hiding the title bar has to happen before the view is created
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);
    // ...
}