动作条微调定制 [英] Actionbar spinner customisation
问题描述
我想自定义Android的动作条微调的东西,像谷歌电流应用。基本上,只有副标题应反映我从微调选择,而标题保持不变。据我所知,一个自定义的微调需要创建,我必须重写 getView()
和 getDropDownView()
方法。但是,我在这里很困惑如何正确地重写这些方法。可一些请轻推我朝着正确的方向。我希望我做了我的问题清楚了。
下面给出我的code。
公共类CustomSpinnerAdapter扩展了BaseAdapter {
私人LayoutInflater充气;
私人最终上下文的背景下;
私人最终的String []下拉菜单;
私人最终字符串的mainText;
私人最终字符串的潜台词;
公共CustomSpinnerAdapter(上下文的背景下,
字符串的mainText,潜台词的字符串,字符串[]下拉){
充气=(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mainText =的mainText;
this.subText =潜台词;
this.context =背景;
this.dropDown =下拉菜单;
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回0;
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回null;
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回0;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
查看actionBarView = inflater.inflate(R.layout.custom_spinner,NULL);
TextView中的TextView =(TextView中)actionBarView
.findViewById(R.id.custom_spinner_textview);
textView.setText(的mainText);
返回actionBarView;
}
@覆盖
公共查看getDropDownView(INT位置,查看convertView,ViewGroup中父){
查看dropDownView = inflater.inflate(R.layout.custom_spinner,NULL);
TextView的dropDownTextView =(TextView中)dropDownView
.findViewById(R.id.custom_spinner_dropdown_textview);
dropDownTextView.setText(下拉[位置]);
返回dropDownView;
}
}
我解决了这个问题。
我的适配器类上市:
公共类AdapterBaseMaps扩展了BaseAdapter {
上下文语境;
INT layoutResourceId;
ArrayList的< ObjectLayers>数据;
LayoutInflater充气;
公共AdapterBaseMaps(上下文的背景下,INT textViewResourceId,
ArrayList的< ObjectLayers>数据) {
//超级(一个,textViewResourceId,数据);
this.data =数据;
充气=(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context =背景;
this.layoutResourceId = textViewResourceId;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看actionBarView = inflater.inflate(R.layout.ab_main_view,NULL);
TextView的标题=(TextView中)actionBarView
.findViewById(R.id.ab_basemaps_title);
TextView的字幕=(TextView中)actionBarView
.findViewById(R.id.ab_basemaps_subtitle);
title.setText(context.getResources()
.getString(R.string.label_cartravel));
subtitle.setText(data.get(位置).getLayerName());
返回actionBarView;
}
@覆盖
公共查看getDropDownView(INT位置,查看convertView,ViewGroup中父){
查看actionBarDropDownView = inflater.inflate(
R.layout.ab_dropdown_view,NULL);
TextView的dropDownTitle =(TextView中)actionBarDropDownView
.findViewById(R.id.ab_basemaps_dropdown_title);
dropDownTitle.setText(data.get(位置).getLayerName());
返回actionBarDropDownView;
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回data.size();
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回null;
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回0;
}
}
ab_main_view.xml上市:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
<的TextView
机器人:ID =@ + ID / ab_basemaps_title
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=TextView的
机器人:TEXTSIZE =20SP
机器人:文字颜色=@色/白/>
<的TextView
机器人:ID =@ + ID / ab_basemaps_subtitle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / ab_basemaps_title
机器人:文本=TextView的
机器人:文字颜色=@色/白
机器人:TEXTSIZE =13SP/>
< / RelativeLayout的>
ab_dropdown_view.xml上市:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
<的TextView
机器人:ID =@ + ID / ab_basemaps_dropdown_title
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:重力=中心
机器人:TEXTSIZE =20SP
机器人:填充=5DP
机器人:文字颜色=@色/白/>
< / RelativeLayout的>
I am trying to customise the android actionbar spinner to something that like the google currents application. Basically, only the 'subtitle' should reflect what i choose from the spinner while the 'title' remains the same. I understand that a custom spinner needs to be created and I have to override the getView()
and getDropDownView()
method. But I am very confused here on how to override these methods properly. Can some please nudge me in the right direction. I hope I made my question clear.
Given below is my code.
public class CustomSpinnerAdapter extends BaseAdapter {
private LayoutInflater inflater;
private final Context context;
private final String[] dropDown;
private final String mainText;
private final String subText;
public CustomSpinnerAdapter(Context context,
String mainText, String subText,String[] dropDown) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mainText=mainText;
this.subText=subText;
this.context = context;
this.dropDown=dropDown;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View actionBarView = inflater.inflate(R.layout.custom_spinner, null);
TextView textView = (TextView) actionBarView
.findViewById(R.id.custom_spinner_textview);
textView.setText(mainText);
return actionBarView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View dropDownView = inflater.inflate(R.layout.custom_spinner, null);
TextView dropDownTextView = (TextView) dropDownView
.findViewById(R.id.custom_spinner_dropdown_textview);
dropDownTextView.setText(dropDown[position]);
return dropDownView;
}
}
I solved it.
Listing of my adapter class:
public class AdapterBaseMaps extends BaseAdapter {
Context context;
int layoutResourceId;
ArrayList<ObjectLayers> data;
LayoutInflater inflater;
public AdapterBaseMaps(Context context, int textViewResourceId,
ArrayList<ObjectLayers> data) {
// super(a, textViewResourceId, data);
this.data = data;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.layoutResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View actionBarView = inflater.inflate(R.layout.ab_main_view, null);
TextView title = (TextView) actionBarView
.findViewById(R.id.ab_basemaps_title);
TextView subtitle = (TextView) actionBarView
.findViewById(R.id.ab_basemaps_subtitle);
title.setText(context.getResources()
.getString(R.string.label_cartravel));
subtitle.setText(data.get(position).getLayerName());
return actionBarView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View actionBarDropDownView = inflater.inflate(
R.layout.ab_dropdown_view, null);
TextView dropDownTitle = (TextView) actionBarDropDownView
.findViewById(R.id.ab_basemaps_dropdown_title);
dropDownTitle.setText(data.get(position).getLayerName());
return actionBarDropDownView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
Listing of ab_main_view.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/ab_basemaps_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"
android:textColor="@color/White" />
<TextView
android:id="@+id/ab_basemaps_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ab_basemaps_title"
android:text="TextView"
android:textColor="@color/White"
android:textSize="13sp" />
</RelativeLayout>
Listing of ab_dropdown_view.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/ab_basemaps_dropdown_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:padding="5dp"
android:textColor="@color/White" />
</RelativeLayout>
这篇关于动作条微调定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!