van*_*dzi 11 android android-layout android-actionbar
默认情况下,在操作栏列表中,所选项目的导航宽度与最宽项目一样宽.我想在所选项目中"wrap_content",因为它在谷歌+,gmail,映射Android应用程序.
有人知道这么热吗?
我试图覆盖导航适配器的getView但它不起作用.它看起来这个视图被包含在另一个视图中,该视图的宽度最宽.我也尝试了带有微调器的actionbar.setCustom视图,但是微调器的行为是相同的.它使用最宽的项目宽度.
感谢所有建议,链接和帮助.
Wei*_*ang 13
我遇到了同样的问题.看来Android会为所有项调用getView()方法,最后将宽度设置为最宽项的宽度.
所以这是我的解决方案:
在代码中存储当前选定的部分(例如,示例中的部分),例如selectedSection.实际上你必须在NavigationListener的onNavigationItemSelected()方法中完成它.
覆盖SpinnerAdapter的getView()方法,始终将其内容设置为selectedSection.
在NavigationListener的onNavigationItemSelected()方法中,在将当前模式设置为selectedSection后,尝试调用spinnerAdapter的notifyDataSetChanged()方法.
这是示例代码:
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;
}
});
Run Code Online (Sandbox Code Playgroud)