9

我正在尝试包装我的 ActionBar 导航微调器的文本内容(我正在使用 ActionBar Sherlock)。似乎我的微调器采用了下拉列表中包含的项目的宽度。

如何使微调器中的选定项目根据其宽度“包裹”?可以在 GMaps 操作栏微调器中找到一个示例。

4

2 回答 2

1

实际上,android 会尝试为所有项目调用 getView(),并将微调器的宽度设置为最宽。请参考我在另一个线程中的解决方案。https://stackoverflow.com/a/15356679/2159849

于 2013-03-12T08:49:58.383 回答
1

更新:

在此处下载我的示例项目:微调器宽度测试(Dropbox 文件夹) (它包含此答案中提供的两种解决方案)。

由于字符的字距调整(字母间距)因文本而异,微调器的宽度也改变了宽度我之前发布的答案(下)

所以不用子串标题,只需像这样设置 TextView 的宽度(以像素为单位):

textView.setWidth(200);
  • 文本现在将被截断并以“..”结尾。

  • 微调器的宽度保持不变。

  • 您可以为下拉列表和微调器设置不同的宽度

  • 您仍然需要自定义 SpinnerAdapter,但不需要自定义
    SpinnerItem 类,只需给适配器一个 String[] 数组即可。

spinner.setAdapter(new TruncatedSpinnerAdapter(stringArray));//String[]

在 TruncatedSpinnerAdapter 中:

public class TruncatedSpinnerAdapter implements SpinnerAdapter {

    String[] spinnerItem;

    public TruncatedSpinnerAdapter(String[] spinnerItem) {
        this.spinnerItem = spinnerItem;
    }

//    ...more required interface callbacks here...

    /**
     * Returns the View that is shown when a spinner item is selected.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = getLayoutInflater().inflate(
                android.R.layout.simple_spinner_item, null);
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        textView.setText(spinnerItem[position]);
        //Set the width of the TextView in pixel. 
        //the text will now get truncated and ending with ".."
        textView.setWidth(200);
        return textView;
    }

更新前的回答:

您可以通过创建自定义 SpinnerAdapter 来包装文本,您可以在其中控制在微调器视图和微调器下拉视图中显示的文本的长度。

 /**
     * A SpinnerItemAdapter to handle SpinnerItem Objects, 
     * displays the ArrayList of SpinnerItem Objects.
     */
    public class SpinnerItemAdapter implements SpinnerAdapter{

        /**
         * The internal data, ArrayList of SpinnerItem Objects.
         */
        SparseArray<SpinnerItem> spinnerItem;

        public SpinnerItemAdapter(SparseArray<SpinnerItem> spinnerItem){
            this.spinnerItem = spinnerItem;
        }

        /**
         * Returns the Size
         */
        @Override
        public int getCount() {
            return spinnerItem.size();
        }
        /**
         * Returns a SpinnerItem Object at the specified position.
         */
        @Override
        public Object getItem(int position) {
            return spinnerItem.valueAt(position);
        }


//    ...more required interface callbacks here...


        /**
         * Views displayed when the Spinner is clicked, the drop 
         * down list of spinner items.
         */
        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            View view=getLayoutInflater().inflate(android.R.layout.simple_spinner_dropdown_item, null); 
            TextView v=(TextView)view.findViewById(android.R.id.text1);
            v.setText(spinnerItem.valueAt(position).getDropDownTitle());     
            return v;
        }
        /**
         * Returns the View that is shown when a spinner item is selected.
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=getLayoutInflater().inflate(android.R.layout.simple_spinner_item, null); 
            TextView v=(TextView)view.findViewById(android.R.id.text1);
            v.setText(spinnerItem.valueAt(position).getShortTitle());   
            return v;
        }

    }

使用自定义 SpinnerItem 类填充适配器,该类可以保存并返回微调器视图的缩短标题。

class SpinnerItem {

    // SpinnerItem fields, including variable of type SpinnerItem
    public String title = "";
    // sets the width of the spinnerItem
    public int titleLength = 10;//default value
    public int titleDropDownLength = 20;

    public long id;

    // SpinnerItem methods()

    /**
     * Title with max characters displayed, set by titleLength;
     * 
     * @return title of the spinnerItem.
     */
    public CharSequence getShortTitle() {
        if (title.length() == 0)
            return "?";//
        else if (title.length() > 0 && title.length() <= titleLength) {
            return title;
        } else {
            String shortTile = title.substring(0, titleLength).trim() + "..";
            return shortTile;
        }
    }

    public CharSequence getDropDownTitle() {
        if (title.length() == 0)
            return "?";//
        else if (title.length() > 0 && title.length() <= titleDropDownLength) {
            return title;
        } else {
            String shortTile = title.substring(0, titleDropDownLength).trim() + "..";
            return shortTile;
        }
    }
}

使用 SpinnerItem 类,您可以通过在创建 SpinnerItem 对象时设置 MAX 标题长度来控制微调器项目视图中显示的标题长度。

//Create an ArrayList for the Adapter with SpinnerItems
        SparseArray<SpinnerItem> spinnerItems = new SparseArray<SpinnerItem>();

        //Some dummy Cheese titles for the spinner items
        String[] sCheeseStrings = {
               "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
               "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale"};

        for (int i = 0; i < sCheeseStrings.length; i++) {
            SpinnerItem spinnerItem= new SpinnerItem(); 
            spinnerItem.title=sCheeseStrings[i];
            spinnerItem.id=i;
            spinnerItem.titleLength=MAX_TITLE_LENGTH;
            spinnerItem.titleDropDownLength=MAX_DROP_DOWN_TITLE_LENGTH;
            spinnerItems.append(i, spinnerItem);
        }
        spinnerAdapter = new SpinnerItemAdapter(spinnerItems);

然后将 spinnerAdapter 添加到 Spinner

spinner.setAdapter(spinnerAdapter);

ActionBar:menu.xml 示例中的微调器

        // For API below 11 use ActionBar Sherlock with Android Support Library
//       getSupportMenuInflater().inflate(R.menu.activity_menu, menu);
        // For API above 11
        getMenuInflater().inflate(R.menu.activity_menu, menu);
        spinner_menu = (Spinner) menu.findItem(R.id.menu_spinner).getActionView();
        spinner_menu.setAdapter(spinnerAdapter);
于 2012-10-06T12:14:42.260 回答