将微调器添加到操作栏右侧时出错

时间:2014-10-28 07:15:15

标签: android

收到以下错误 类型不匹配:无法从android.support.v7.app.ActionBar.LayoutParams转换为android.app.ActionBar.LayoutParams

LayoutInflater inflater = (LayoutInflater) getActionBar().getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);

            final View spinnerView = inflater.inflate(R.layout.layout_spinner, null);
            Spinner spinner = (Spinner) spinnerView.findViewById(R.id.my_spinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Location, R.layout.spinner_item);
            adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
            spinner.setAdapter(adapter);

//          spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//              @Override
//              public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                  // Do whatever you want with your selected item. You can get it as: parent.getItemAtPosition(position); 
//              }
//
//              @Override
//              public void onNothingSelected(AdapterView<?> parent) {}
//          });

            getActionBar().setIcon(getResources().getDrawable(R.drawable.loc));//set your actionbar logo
            getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE );

            LayoutParams layoutParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams.gravity = Gravity.RIGHT; // set your layout's gravity to 'right'
            getActionBar().setCustomView(spinnerView, layoutParams); //place your layout on the actionbar

我在new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);

收到错误

请帮助...我无法解决错误,因为我是Android新手

2 个答案:

答案 0 :(得分:1)

您必须导入正确的LayoutParams

删除:

import android.app.ActionBar.LayoutParams 

添加:

import android.support.v7.app.ActionBar.LayoutParams 

如果仍然出现异常,请反之亦然

答案 1 :(得分:1)

如果您使用的是支持jar,请更改

import android.app.ActionBar.LayoutParams

import android.support.v7.app.ActionBar.LayoutParams

如果不是,那反之亦然。