Android 使用NavigationUI设置工具栏
我正在尝试为我的简单导航UI设置一个工具栏,它既没有底部导航也没有抽屉 这是我的主要活动布局的代码Android 使用NavigationUI设置工具栏,android,android-fragments,fragment,android-toolbar,android-navigation-graph,Android,Android Fragments,Fragment,Android Toolbar,Android Navigation Graph,我正在尝试为我的简单导航UI设置一个工具栏,它既没有底部导航也没有抽屉 这是我的主要活动布局的代码 <?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=&qu
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorTopNavigation"
android:theme="@style/Theme.AppCompat.Light"
app:title="Testing"
/>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.appcompat.widget.LinearLayoutCompat>
导航图是
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/reader_fragment">
<fragment
android:id="@+id/reader_fragment"
android:name="com.example.quran.ReaderFragment"
android:label="Reader"
tools:layout="@layout/reader_fragment">
<action
android:id="@+id/action_chapters_to_chapterDetails"
app:destination="@id/chapterDetailsFragment" />
</fragment>
<fragment
android:id="@+id/chapterDetailsFragment"
android:name="com.example.quran.ChapterDetailsFragment"
android:label="fragment_chapter_details"
tools:layout="@layout/fragment_chapter_details" />
</navigation>
我已经通过设置
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorTopNavigation</item>
<item name="colorPrimaryDark">@color/colorTopNavigation</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
然后,回收器视图如下所示
但是我想要回收器视图和工具栏,我还想要自定义工具栏,有点像自定义字体。有人能指导我吗
感谢您将android:orientation=“vertical”添加到您的主要活动中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorTopNavigation"
android:theme="@style/Theme.AppCompat.Light"
app:title="Testing" />
<fragment android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.appcompat.widget.LinearLayoutCompat
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorTopNavigation</item>
<item name="colorPrimaryDark">@color/colorTopNavigation</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// val toolbar = findViewById(R.id.toolbar) as Toolbar
// setSupportActionBar(toolbar)
// val navHostFragment: NavHostFragment = nav_host_fragment as NavHostFragment
// NavigationUI.setupWithNavController(toolbar, navHostFragment.navController)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.add(ReaderFragment(), ReaderFragment::javaClass.toString())
.commit()
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorTopNavigation"
android:theme="@style/Theme.AppCompat.Light"
app:title="Testing" />
<fragment android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.appcompat.widget.LinearLayoutCompat