如何在DrawerItems中使用TypeFace?

时间:2015-06-01 04:53:22

标签: android typeface

我正在使用android studio。我想用zawgyi字体;改变“Android”文字。我无法使用此代码, 字体字体= Typeface.createFromAsset(getAssets(),“fonts / zawgyi.ttf”);

MainActivity.java

private void addDrawerItems() {
        String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
        mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          //      Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
            }
        });
    }

activity_main.xml中

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The first child in the layout is for the main Activity UI-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffffff"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="100dp"
            android:gravity="center"
            android:text="Holy Operating Systems, Batdroid!"
            android:textSize="24sp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/batdroid" />

    </RelativeLayout>

    <!-- Side navigation drawer UI -->
    <ListView
        android:id="@+id/navList"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#ffeeeeee" />

</android.support.v4.widget.DrawerLayout>

3 个答案:

答案 0 :(得分:0)

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

    }
}

然后你从这个&#34; listAdapter&#34;创建一个新的适配器。上课,或任何你想要的名字。然后你可以使用这个适配器设置你的列表视图,你应该很高兴。

您可以关注此Link

答案 1 :(得分:0)

使用getView()方法调用Typeface

@Override
public View getView(int position, View convertView, ViewGroup parent) {

            convertView = a.getLayoutInflater().inflate(R.layout.listView, null);

            Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/zawgyi.ttf");
            TextView tv = (TextView) vi.findViewById(R.id.textview); 
            tv.setTypeface(tf);

            return convertView;

        }

答案 2 :(得分:0)

       Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/oneway.ttf");
TextView textview=findViewById(R.id.textviewname);
           textview.setTypeface(typeface);