问题
I want to set the custom font for spinner.
Here is my pojo class, which is used as spinner item.
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;
}
}
This is my spinner drop down custom item layout.
<?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" />
In my Activity class i am setting the adapter like this.
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);
}
回答1:
Add this in your style:
<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:
You have to make a custom spinner to change the font of Spinner, see below as example.
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;
}
}
回答3:
Just follow below steps which will help to you. and Custom adapter contains custom fonts for textview. Follow below steps.
1.Create Activity i.e MainActivity.java contains
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.layout file activity_main.xml with
<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.Now Create Class For City i.e 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.Need to create adapter for city and which contains custom fonts 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.with lyout for adapter city_adapter.xml below
<?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>
You have to place your font in assets/fonts/xyz.ttf for my case font is roboto_bold.ttf which is at assets/fonts/roboto_bold.ttf and working fine. Just add your city object in list.
来源:https://stackoverflow.com/questions/25398296/custom-font-for-spinner-in-android