MainActivity 主题 NoActionBar 但片段仍显示 NoActionBar

MainActivity theme NoActionBar but fragments still showing NoActionBar

我试图隐藏工具栏以便我可以放入自己的工具栏,但工具栏仍然显示,请有人帮忙。主题 @style/AppTheme.NoActionBar 适用于其他活动。

清单:

<activity android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="adjustPan"/>

风格:

 <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Activity:

class MainActivity : AppCompatActivity() {

private val fireStoreInstance: FirebaseFirestore by lazy { FirebaseFirestore.getInstance() }
private val currentUserDocRef: DocumentReference
    get() = fireStoreInstance.document("users/${FirebaseAuth.getInstance().currentUser?.uid
            ?: throw NullPointerException("UID is null.")}")
internal lateinit var helper: RealmHelper
internal lateinit var realm: Realm

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    Realm.init(applicationContext)
    val defaultConfig = RealmConfiguration.Builder()
            .schemaVersion(0)
            .build()
    realm = Realm.getInstance(defaultConfig)
    helper = RealmHelper(realm)
    fireStoreInstance.collection("users").document(FirebaseAuth.getInstance()
            .currentUser?.uid.toString()).get().addOnCompleteListener { task ->
        if (task.isSuccessful) {
            AppConstants.USER_ROLE = task.result["role"].toString()
        }
    }
    val list = helper.retrieveAT()
    if (list.count() <= 0) {
        fireStoreInstance.collection("assessmentType").get().addOnCompleteListener(OnCompleteListener<QuerySnapshot> { task ->
            if (task.isSuccessful) {
                val list = ArrayList<AssessmentType>()
                for (document in task.result) {
                    val u = AssessmentType(document.id,document.get("Desc")!!.toString())
                    helper.save(u)
                }
            }
        })
    }
    this.replaceFragment(PeopleFragment())
    navigation.setOnNavigationItemSelectedListener {
        when (it.itemId) {
            R.id.navigation_chat -> {
                this.replaceFragment(PeopleFragment())
                true
            }
            R.id.navigation_modules -> {
                this.replaceFragment(ModuleFragment())
                true
            }
            R.id.navigation_timetable -> {
                this.replaceFragment(TimetableFragment())
                true
            }
            R.id.navigation_profile -> {
                this.replaceFragment(MyAccountFragment())
                true
            }
            else -> false
        }
    }
}

@SuppressLint("CommitTransaction")

private fun replaceFragment(fragment: Fragment) {
    supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_layout, fragment)
            .commit()
}

}

屏幕:

您可以使用以下代码隐藏操作栏

getActivity().getSupportActionBar().hide();

您的主题不完整,类似于

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

以上样式parent="Theme.AppCompat.Light.NoActionBar"是您解决问题所需要的。


所以你的风格一定是

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

</style>

这对我来说适用于每个应用...

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>

</style>