简体   繁体   中英

MainActivity theme NoActionBar but fragments still showing NoActionBar

I'm trying to hide the Toolbar so I can put my own one in but the toolbar still shows, could someone help please. The theme @style/AppTheme.NoActionBar works in other activities.

Manifest:

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

Style:

 <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()
}

}

Screen:

工具栏

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

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

Your theme is incomplete which would be like

<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>

In above style parent="Theme.AppCompat.Light.NoActionBar" is what you need to solve your issue.


So your style must be

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

</style>

This works for me in every app...

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM