代码之家  ›  专栏  ›  技术社区  ›  TMS

动作条微调器自定义

  •  8
  • TMS  · 技术社区  · 13 年前

    我正在尝试将android操作条微调器定制为类似谷歌当前应用程序的东西。基本上,只有“副标题”应该反映我从微调器中选择的内容,而“标题”保持不变。我知道需要创建一个自定义微调器,并且我必须覆盖 getView() getDropDownView() 方法但我在这里对如何正确地覆盖这些方法感到非常困惑。有人能把我推向正确的方向吗。我希望我把问题说清楚了。

    the image screenshot
    (来源: androidcowboy.com )

    下面给出的是我的代码。

    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;
    
    }
    }
    
    2 回复  |  直到 7 年前
        1
  •  17
  •   lmaooooo    13 年前

    我解决了。

    我的适配器类的列表:

    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;
    }
    
    }
    

    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>
    

    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>
    
        2
  •  2
  •   anthropomo    13 年前

    第一个答案很有帮助,但Adapter子类是不必要的。

    将XML定义为@user1624587的答案,然后简单地在中引用它 public boolean onCreateOptionsMenu(Menu menu) :

        ArrayAdapter<CharSequence> someAdapter = new
            ArrayAdapter<CharSequence>(context, R.layout.ab_main_view, 
            android.R.id.text1, getResources().getStringArray(R.array.some_array));
    
    
        someAdapter.setDropDownViewResource(R.layout.ab_dropdown);
    
        MenuItem item = menu.add("SomeTitle").setActionView(R.layout.some_spinner);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); //or whatever
    
        someSpinner = (someSpinner) item.getActionView();
        someSpinner.setAdapter(someAdapter);
        ...etc