![](/img/trans.png)
[英]How to customize item background and item text color inside NavigationView?
[英]Customize NavigationView item
如何在android支持設計庫中自定義NavigationView項? 例如,我想自定義項目的文本視圖或項目的圖像視圖我想為導航視圖列表的每一行添加 xml 布局,例如 ListView 適配器。 請幫我。
您可以完全為導航視圖創建自定義視圖。 在下面的示例中,我在導航視圖中使用片段容器,然后根據需要自定義片段。
在您的活動布局中
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view_controllers.diagrams.DiagramEditorScreen"
android:id="@+id/diagramEditorMainDrawerLayout"
>
.... {your activity layout}
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/diagramEditorSlideInMenuNavigationView"
app:itemTextColor="@color/white"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/diagramEditorSlideInMenuFragmentHolder"
>
</FrameLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
我在導航視圖中使用的片段布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tools"
android:textAllCaps="true"
android:gravity="start"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="2dp"
android:layout_marginEnd="10dp"
android:fontFamily="@font/trade_gothic_next_lt_pro_bd"
android:textColor="@color/mediumGray"
android:textSize="16sp"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/mediumGray"
android:layout_marginStart="10dp"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/fragmentBrushesRecyclerView"
android:layout_weight="1"
android:layout_marginTop="10dp"
>
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
在您的 Activity onCreate
方法中添加偵聽器
val actionDrawerToggle = ActionBarDrawerToggle(this, diagramEditorMainDrawerLayout, R.string.open, R.string.close)
diagramEditorMainDrawerLayout.addDrawerListener(actionDrawerToggle)
actionDrawerToggle.syncState()
當您准備好顯示 navigationView 時,您只需替換 navigationView 和openDrawer()
的片段openDrawer()
。 我使用此功能將 navigationView 替換為幾個不同的片段。
supportFragmentManager.beginTransaction()
.replace(R.id.diagramEditorSlideInMenuFragmentHolder, {your-fragment}, {your-fragment-tag})
.commit()
diagramEditorMainDrawerLayout.openDrawer(GravityCompat.START)
注意:以上是用androidX完成的。 如果您沒有使用 androidX,則您對 navigationView 的布局調用應該是
<android.support.v4.widget.DrawerLayout
....
>
<android.support.design.widget.NavigationView
...
/>
</android.support.v4.widget.DrawerLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.