Android 如何更改自定义导航抽屉的字体?
这是我的导航抽屉的布局:Android 如何更改自定义导航抽屉的字体?,android,Android,这是我的导航抽屉的布局: <?xml version="1.0" encoding="utf-8"?> <!-- the root view is now a LinearLayout, all other Views are children of this --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_heig
<?xml version="1.0" encoding="utf-8"?>
<!-- the root view is now a LinearLayout, all other Views are children of this -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#121314"
android:orientation="vertical">
<!-- a separate section to go above the list -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp">
<!-- your image, you can set it later (see NavDrawerFrag) -->
<ImageView
android:id="@+id/nav_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:padding="15dp"
android:src="@android:drawable/ic_menu_myplaces"/>
<!-- a bit of test or a title to go with it
<TextView
android:id="@+id/nav_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Default text"/>-->
</LinearLayout>
<!-- some divider thing
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:padding="20dp"
android:background="#000000"/>-->
<!-- your ListView is now a child View -->
<ListView
android:id="@+id/nav_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:listSelector="@drawable/colors"/>
</LinearLayout>
您应该创建自己的textview xml布局,而不是使用
android.R.id.text1
作为textview资源。你可以这样做
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id= "@+id/listView >
<TextView
android:id="@+id/listItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView
</LinearLayout>
然后,您可以从这个“listAdapter”类或任何您想命名的类创建一个新适配器。然后你可以用这个适配器设置你的listview,你就可以开始了。和的副本,更不用说通过在一个主要的Web搜索引擎中搜索
listview自定义字体可以在网上找到的许多其他资源了。可能是,但问题是如何在我的自定义方案中实现它。你的“自定义方案”是什么意思?你不能通过样式资源设置自定义字体。我按照你的建议做了,但用java更改了字体<代码>字体tf=Typeface.createFromAsset(getAssets(),“fonts/Raleway Thin.otf”);TextView tv=(TextView)findViewById(R.id.listItem);电视设置字体(tf)代码>现在它不断抛出NullPointerException
字体部分应该可以正常工作。获取null的原因是您正在访问的视图不是R.id.listView
。因此,它试图在主视图中查找R.id.listItem
,而它不在主视图中。我意识到,只有在使用自定义适配器设置导航抽屉时,这才有效。你以前做过吗?哈哈,所以它非常复杂,所以你知道,但是如果你想控制导航抽屉中的每个项目的功能,你需要一个自定义适配器。我会将你需要的课程添加到我的答案中,这样你就可以开始了。我可能忘记了适配器代码中的某些内容,但它应该足够近。这将取代mDrawerListView.setAdapter(新的ArrayAdapter(getActionBar().getThemedContext(),R.layout.listitem,R.id.listitem,siteNames))代码>?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id= "@+id/listView >
<TextView
android:id="@+id/listItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView
</LinearLayout>
public class listAdapter extends BaseAdapter {
String[] siteNames;
Activity a;
public listAdapter(Activity a, String[] siteNames) {
this.a = a;
this.siteNames = siteNames;
}
public int getCount() {
return siteNames.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
vi = a.getLayoutInflater().inflate(R.layout.listView, null);
Typeface tf = Typeface.createFromAsset(a.getAssets(), "fonts/Raleway-Thin.otf");
TextView tv = (TextView) vi.findViewById(R.id.listItem);
tv.setTypeface(tf);
//whatever other changes you want to make to your list items.
return vi;
}
}