【问题标题】:How can I change the font of customised Navigation Drawer?如何更改自定义导航抽屉的字体?
【发布时间】:2014-06-29 02:48:15
【问题描述】:

这是我的导航抽屉的布局:

<?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>

我想在ListView 中使用自定义字体,但我已经为此苦恼了两天。我似乎无法让它工作。

这是创建导航抽屉的部分:

public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState)
{
    // need site names for list
    siteNames = getActivity().getResources().getStringArray(R.array.site_names);
    Log.d(TAG, "number of sites loaded: " + siteNames.length);

    // inflate the parent view (the entire layout)
    View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    // now grab the separate child views from inside it
    mDrawerListView = (ListView) view.findViewById(R.id.nav_listView);
    mDrawerImage = (ImageView) view.findViewById(R.id.nav_image);
    //mDrawerText = (TextView) view.findViewById(R.id.nav_text);

    // configure the Views
    mDrawerImage.setImageResource(R.drawable.orange);
    mDrawerListView.setOnItemClickListener(this);
    mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar().getThemedContext(),
            android.R.layout.simple_list_item_1, android.R.id.text1, siteNames));

    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);

    // and return the inflated view up the stack
    return view;
}

【问题讨论】:

标签: android


【解决方案1】:

您应该创建自己的 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>

以下是您需要的自定义适配器,因此您可以将自定义样式设置为您的各个项目。

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;

    }


}

然后你从这个“listAdapter”类中创建一个新的适配器,或者任何你想命名的。然后你可以用这个适配器设置你的列表视图,你应该很高兴。

【讨论】:

  • 您不能通过样式资源设置自定义字体。
  • 我按照你的建议做了,但是用 java 改变了字体。 Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Raleway-Thin.otf"); TextView tv = (TextView) findViewById(R.id.listItem); tv.setTypeface(tf); 现在它一直在抛出 NullPointerException
  • 字体部分应该可以正常工作。您正在访问的视图不是 R.id.listView 的原因是 null。所以它试图在主视图内找到R.id.listItem,它不在主视图内。我意识到这只有在您使用自定义适配器来设置导航抽屉时才有效。你以前做过吗?
  • 哈哈,所以你知道它非常复杂,但是如果你想控制导航抽屉中每个项目的作用,你需要一个自定义适配器。我会将您需要的课程添加到我的答案中,以便您开始学习。我可能忘记了适配器代码中的某些内容,但它应该足够接近。
  • 这会代替mDrawerListView.setAdapter(new ArrayAdapter&lt;String&gt;(getActionBar().getThemedContext(), R.layout.listitem, R.id.listItem, siteNames));?
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多