我正在尝试实施微调器在其上方具有品牌名称的操作栏中。 使用ActionBar setListNavigationCallbacks方法(如果可能)
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(mSpinnerAdapter, null);
android http://i40.tinypic.com/11i332v.png
任何人都可以告诉我如何做到这一点吗?
我会在这里放一些代码,但我不知道从哪里开始,因为我还没有设法找到相关信息。
编辑:使用V4.0
答案 0 :(得分:0)
我已经设法自己做了,并且只是在有人想要答案的情况下发布答案。
如果有人想指出一种更优雅的方式,我很乐意听到它。除了我自己的答案之外,不会有人能提出更好的答案。
//In OnCreate method on my activity I've got
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("");
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
final SpinnerAdapter madap = new EntArrayAdapter(this,R.layout.ent_ddl_actionbar, new ArrayList<EntArrayAdapter.Data>(),allents);
//ActionBar.OnNavigationListener nav will normaly be in here instead of null
actionBar.setListNavigationCallbacks(madap, null);
}
我的EntAdapter看起来像这样:
public class EntArrayAdapter extends ArrayAdapter<com.SimpleEID.widget.EntArrayAdapter.Data> {
private Context _context;
private List<Data> _objects;
public class Data implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1
public Data( Ent e) {
this.Text = e.getName();
this.DDText = e.getName();
this.curEnt = e;
}
public Data()
{
this.DDText = "No Avalible Ent";
this.curEnt = null;
}
public String Text;
public String DDText;
public Integer ID;
public Ent curEnt;
}
private int textSize=14;
private int ddLSize=20;
public EntArrayAdapter(final Context context,final int textViewResourceId, List<Data> objects,ArrayList<Ent> entList) {
super(context, textViewResourceId, objects);
this._context = context;
this._objects = objects;
for(int i = 0; i< entList.size() ;i++)
{
Ent e = entList.get(i);
this.add(new Data(e));
}
//Empty case just adding blank
if(entList.size() == 0)
{
this.add(new Data());
}
}
//This part is the Dropdown part of the view.So in here I use layout simple spinner
//As this only needs to show a word
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(_context);
convertView = inflater.inflate(
android.R.layout.simple_spinner_item, parent, false);
}
String dropdownText = _objects.get(position).DDText;
TextView tv = (TextView) convertView
.findViewById(android.R.id.text1);
tv.setText(dropdownText);
// extra settings to my text on dropdown
tv.setHeight(100);
tv.setPadding(40, 20, 0,0);
tv.setTextSize(ddLSize);
return convertView;
}
// This getView is the view that shows before you click to change into a dropdown
// Also the part that shows at the top in the image case it is "BRAND NAME Ent1>"
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(_context);
//Chosen my own layout for this part which is basically 2 textViews inside of a LinearLayout
convertView = inflater.inflate(
R.layout.ent_ddl_actionbar, parent, false);
}
TextView tv = (TextView) convertView
.findViewById(R.id.text_spinner_1);
tv.setText(_objects.get(position).Text);
tv.setTextColor(Color.BLUE);
tv.setTextSize(textSize);// Controls the Non List size
return convertView;
}
}
我的布局
//R.layout.ent_ddl_actionbar is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/bar_title1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/default_brandName"
android:textSize="16sp"/>
<TextView
android:id="@+id/text_spinner_1"
android:layout_height="fill_parent"
android:layout_width="100dip"
android:text="test"
/>
</LinearLayout>
希望这有帮助