android中微调器的自定义字体

时间:2014-08-20 06:43:07

标签: android spinner custom-font

我想为微调器设置自定义字体。

这是我的pojo类,用作微调项目。

public class City {

private String cityId;
private String cityName;

public String getCityId() {
    return cityId;
}
public void setCityId(String cityId) {
    this.cityId = cityId;
}
public String getCityName() {
    return cityName;
}
public void setCityName(String cityName) {
    this.cityName = cityName;
}

@Override
public String toString() {
    return cityName;
}

}

这是我的微调器下拉自定义项目布局。

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF" >

<TextView
    android:id="@+id/league_headr_title"
    android:layout_width="fill_parent"
    android:layout_height="30dp"        
    android:textColor="@color/black"
    android:textSize="15dp"        
    android:text="league"
    android:gravity="left|center_vertical"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" />

在我的Activity类中,我正在设置适配器。

  if(null != cities && cities.size() > 0) {
        ArrayAdapter<City> objectLocationsDataAdapter = new ArrayAdapter<City>    (BloodDonationCenterActivity.this, R.layout.spinnerdroapdown_custom_item,   R.id.league_headr_title, cities);
        Spinner_locations.setAdapter(objectLocationsDataAdapter);
}

enter image description here

3 个答案:

答案 0 :(得分:0)

只需按照以下步骤操作即可。和自定义适配器包含textview的自定义字体。请按照以下步骤操作。

1.创建活动,即 MainActivity.java 包含

package com.example.test;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.listview);

        List<City> cities = new ArrayList<City>();

        for (int i = 1; i <= 10; i++) {
            cities.add(new City(""+i, "City Name "+i));
        }

        CityAdapter adapter = new CityAdapter(getBaseContext(), cities);
        listView.setAdapter(adapter);
    }
}

2.使用

布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

3.现在为城市创建类别,即 City.java

package com.example.test;

public class City {

    private String cityId;
    private String cityName;

    /**
     * @param cityId
     * @param cityName
     */
    public City(String cityId, String cityName) {
        super();
        this.cityId = cityId;
        this.cityName = cityName;
    }
    public String getCityId() {
        return cityId;
    }
    public void setCityId(String cityId) {
        this.cityId = cityId;
    }
    public String getCityName() {
        return cityName;
    }
    public void setCityName(String cityName) {
        this.cityName = cityName;
    }
    @Override
    public String toString() {
        return cityName;
    }
}

4.需要为城市创建适配器并包含自定义字体 CityAdapter.java

package com.example.test;

import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class CityAdapter extends ArrayAdapter<City> {

    private Context mContext;
    private List<City> cities;

    public CityAdapter(Context context,
            List<City> list) {
        super(context, R.layout.city_adapter, list);
        this.mContext = context;
        this.cities = list;
    }

    static class ViewHolder {
        private TextView textView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO getview
        ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.city_adapter, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) convertView.findViewById(R.id.textview);
            viewHolder.textView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/roboto_bold.ttf"));

            convertView.setTag(viewHolder);

        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        City city = cities.get(position);
        if (city != null) {
            viewHolder.textView.setText(city.getCityName());
        }
        return convertView;
    }   
}

5. lyout for city_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dip" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="10dip" />

</RelativeLayout>

您必须将字体放在 assets / fonts / xyz.ttf 中,因为我的案例字体是 roboto_bold.ttf ,位于 assets / fonts / roboto_bold .ttf 并且工作正常。只需在列表中添加您的城市对象即可。

答案 1 :(得分:0)

以您的风格添加:

<style name="Spinner" parent="@android:style/Widget.Spinner">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">#E80243</item>
    <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
</style>
<style name="SpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">

    <item name="android:textAppearance">@style/TextAppearance.SpinnerItem</item>
    <item name="android:textColor">#FFF</item>
</style>
<style name="TextAppearance.SpinnerItem" parent="@android:style/TextAppearance.Widget.TextView.SpinnerItem">
    <item name="android:textColor">#FFF</item>
</style>

答案 2 :(得分:0)

您必须创建一个自定义微调器来更改Spinner的字体,请参见下面的示例。

public class CustomSpinnerAdapter extends ArrayAdapter<String> {
Context ctx;
String[] items;
LayoutInflater mInflater;
int selectedItem = -1;

public CustomSpinnerAdapter(Context context, int textViewResourceId,
        String[] objects, Integer[] image) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
    ctx = context;
    items = objects;

    mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
....
....
....
....

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return getCustomDropDownView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = mInflater.inflate(R.layout.spinner_row, parent, false);

    TextView label = (TextView) row.findViewById(R.id.tv_Time);
    label.setText(items[position]);
    label.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "font.ttf"));

    return row;
}

public View getCustomDropDownView(final int position, View convertView,
        ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = mInflater.inflate(R.layout.spinner_row, parent, false);

    TextView label = (TextView) row.findViewById(R.id.tv_Time);

    // if (position != selectedItem)
    label.setTextColor(Color.BLACK);
    // else
    // label.setTextColor(Color.parseColor("#8cc35e"));

    label.setText(items[position]);
    label.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "font.ttf"));

    return row;
}
}