默认情况下,在选择的项目的动作条列表导航宽度一样宽最宽的项目。 我想选择的项目为“WRAP_CONTENT”,因为它是在谷歌+,Gmail,地图Android应用。
是否有人知道热办呢?
我试图重写导航适配器的getView,但它不工作。 它看起来,这种观点是包裹着具有最宽的项目的宽度另一视图。 我也试过actionbar.setCustom观点与微调,但微调的行为是一样的。 它采用最宽的项目宽度。
感谢所有的建议,链接和帮助。
默认情况下,在选择的项目的动作条列表导航宽度一样宽最宽的项目。 我想选择的项目为“WRAP_CONTENT”,因为它是在谷歌+,Gmail,地图Android应用。
是否有人知道热办呢?
我试图重写导航适配器的getView,但它不工作。 它看起来,这种观点是包裹着具有最宽的项目的宽度另一视图。 我也试过actionbar.setCustom观点与微调,但微调的行为是一样的。 它采用最宽的项目宽度。
感谢所有的建议,链接和帮助。
我曾遇到了同样的问题。 看来Android将调用getView()方法的所有项目,最后设置的宽度最宽的项目的宽度。
因此,这里是我的解决方案:
存储当前选择的部分(例如,在你的榜样节),说selectedSection,在你的代码。 其实你需要做的是在NavigationListener的onNavigationItemSelected()方法。
重写你的SpinnerAdapter的getView()方法,始终设置它的内容selectedSection。
在NavigationListener的onNavigationItemSelected()方法,试着打电话给你spinnerAdapter的notifyDataSetChanged()方法,你设定的当前模式selectedSection后。
下面是示例代码:
final int selectedSection = 0;
final String sectionLabels[] = {"short sec", "long section", "looooonger section"};
final ArrayAdapter<String> arrayAdapter =
new arrayAdapter<String>(this, simple_list_item_1) {
@Override
public int getCount() {
// You need to implement here.
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.you_entry, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.you_text);
// Set the text to the current section.
textView.setText(sectionLabels[selectedSection]);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// You need to implement here.
}
}
actionBar.setListNavigationCallbacks(navigationAdpater,
new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
// Store the current section.
selectedSection = itemPosition;
navigationAdpater.notifyDataSetChanged();
return true;
}
});
你需要重写你的适配器getView()方法,并设置布局PARAMS到TextView的包的内容。 该微调将所选项目自动调整。
我创建了一个自定义适配器从ArrayAdapter延伸:
public class CustomAdapter extends ArrayAdapter<String> {
之后,我重写getView()方法改变当前视图的的LayoutParams:
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View view = convertView;
if (null == view) {
view = LayoutInflater.from(this.getContext())
.inflate(android.R.layout.simple_list_item1, null);
((TextView) view).setText(getItem(position));
}
final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
return view;
}
在您的活动,创建适配器实例,并设置你用它微调器:
List<String> stringList = new ArrayList<String>();
stringList.add("test1");
stringList.add("test12");
stringList.add("test123");
stringList.add("test1234");
final SpinnerAdapter adapter = new CustomAdapter(this.getContext(),
android.R.layout.simple_list_item1,
android.R.id.text1,
stringList);
mySpinner.setAdapter(adapter);
当您选择列表中的东西,微调将重新创建视图,选择的项目重新排序到第一位置,并调整他自我内容的大小。
创建一个微调,并添加textviews了这一点。 在微调器设置为你想要的任何值maxWidth场,并且将处理这个问题。 另一种办法是设置
android:layout_width="0"
android_layout_weight="0.3"
这将意味着,旋转器将占据scrren宽度的1 /第三。
在TextView中您将设置:
android:singleLine="false"
android:ellipsize="end"
这为我工作。 重要的部分是这样的。 把下面上面的代码是从对象的数组中选择文本您的适配器和使用selectedItemPosition。
int selectedItemPosition = position;
if (parent instanceof AdapterView) {
selectedItemPosition = ((AdapterView) parent)
.getSelectedItemPosition();
}
实施例在下面给出。
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
final View spinnerCell;
if (convertView == null) {
// if it's not recycled, inflate it from layout
spinnerCell = inflater.inflate(R.layout.layout_spinner_cell, parent, false);
} else {
spinnerCell = convertView;
}
int selectedItemPosition = position;
if (parent instanceof AdapterView) {
selectedItemPosition = ((AdapterView) parent)
.getSelectedItemPosition();
}
TextView title = (TextView) spinnerCell.findViewById(R.id.spinnerTitle);
title.setText(titles[selectedItemPosition]);
return spinnerCell;
}
如果你需要一个解释请点击此链接: http://coding-thoughts.blogspot.in/2013/11/help-my-spinner-is-too-wide.html