中的NavigationView項目在導航視圖中,當我選擇一個項目時,所選項目應該以粗體顯示並增加字體大小。我如何實現這一目標?將字體大小添加到android
基本上,我想爲導航項目添加一個選擇器,該選擇器將在選定狀態下顯示粗體字體,並在非選中狀態下顯示普通字體。
感謝
中的NavigationView項目在導航視圖中,當我選擇一個項目時,所選項目應該以粗體顯示並增加字體大小。我如何實現這一目標?將字體大小添加到android
基本上,我想爲導航項目添加一個選擇器,該選擇器將在選定狀態下顯示粗體字體,並在非選中狀態下顯示普通字體。
感謝
嘗試像下面
//put this in main Activity
private void setFontToMenuItem(MenuItem mi) {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/customfont.ttf");
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new CustomTypefaceSpan("" , font), 0 , mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}
使用這樣
navigationView = (NavigationView) findViewById(R.id.navigation_view);
Menu m = navigationView.getMenu();
for (int i=0;i<m.size();i++) {
MenuItem item = m.getItem(i);
//for aapplying a font to subMenu ...
SubMenu subMenu = item.getSubMenu();
if (subMenu!=null && subMenu.size() >0) {
for (int j=0; j <subMenu.size();j++) {
MenuItem subMenuItem = subMenu.getItem(j);
setFontToMenuItem(subMenuItem);
}
}
//the method we have create in activity
setFontToMenuItem(item);
}
Styles.xml
<style name="NavDrawerTextStyle" parent="Base.TextAppearance.AppCompat">
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:textSize">30sp</item>
<item name="android:textStyle">bold</item>
</style>
,然後在NavigationView:
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
...
app:itemTextAppearance="@style/NavDrawerTextStyle"
/>
</android.support.v4.widget.DrawerLayout>
我想爲導航項目創建一個選擇器。我想爲選定的項目和非選定的項目選擇不同的文字大小和樣式 – Divya
嘗試這樣http://stackoverflow.com/questions/33140138/changing-the-item-text-size-in-navigation-view –
我想創建一個導航項的選擇。我想爲選定的項目和非選定的項目不同的文本大小和樣式 – Divya
請參閱http://stackoverflow.com/questions/31221637/android-navigation-view-item-menu-background-color –