我正在尝试为既没有底部导航也没有抽屉的简单导航 UI 设置工具栏。
这是我的主要活动布局的代码。
<?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>
MainActivity 的代码
class MainActivity : AppCompatActivity(),
ReaderFragment.OnFragmentInteractionListener {
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)
}
override fun onAttachFragment(fragment: Fragment) {
super.onAttachFragment(fragment)
if (fragment is ReaderFragment) {
} else if (fragment is ChapterDetailsFragment) {
}
}
override fun onFragmentInteraction(uri: String) {
Toast.makeText(this, uri, Toast.LENGTH_SHORT).show()
}
}
导航图是
<?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>
和读者片段是
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Activities that contain this fragment must implement the
* [ReaderFragment.OnFragmentInteractionListener] interface
* to handle interaction events.
* Use the [ReaderFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class ReaderFragment : Fragment(), View.OnClickListener, QuranElementClickListener {
private lateinit var quranElementsListView: RecyclerView
private lateinit var layoutManager: LinearLayoutManager
private lateinit var adapter: QuranTextAdapter
private var param1: String? = null
private var param2: String? = null
private var interactionListener: OnFragmentInteractionListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.reader_fragment, container, false)
val dbAccess = DatabaseAccess.getInstance(context)
dbAccess.open()
val quranElements = dbAccess.allElements
quranElementsListView = view.findViewById(R.id.quranElementsListView)
layoutManager = LinearLayoutManager(context)
quranElementsListView.layoutManager = layoutManager
adapter = QuranTextAdapter(quranElements, this)
quranElementsListView.adapter = adapter
return view
}
override fun onQuranElementClick(quranElement: QuranElement, position: Int) {
print(quranElement.arabicText)
findNavController().navigate(R.id.action_chapters_to_chapterDetails)
}
override fun onClick(v: View?) {
print("This view is clicked")
}
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
interactionListener = context
} else {
throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
interactionListener = null
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
*
*
* See the Android Training lesson [Communicating with Other Fragments]
* (http://developer.android.com/training/basics/fragments/communicating.html)
* for more information.
*/
interface OnFragmentInteractionListener {
// TODO: Update argument type and name
fun onFragmentInteraction(uri: String)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ReaderFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
ReaderFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
我已经通过设置设置了自定义工具栏
<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>
现在,如果我运行该项目,它将仅显示带有阅读器文本的工具栏,并且未加载阅读器片段,并且回收器视图中不显示任何内容。
但是如果我只是在活动布局文件中注释工具栏并将活动中的 onCreate 方法更改为以下
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()
}
}
然后像这样可以看到回收站视图
但我想要回收器视图和工具栏,我还想自定义工具栏,有点像自定义字体。有人可以指导我吗。
谢谢