gpt4 book ai didi

java - 如何在ActionBar中创建二级下拉列表?

转载 作者:行者123 更新时间:2023-12-01 13:50:36 25 4
gpt4 key购买 nike

我有以下代码,它已经创建了一个下拉菜单

public class MainActivity extends FragmentActivity implements
ActionBar.OnNavigationListener {

private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Set up the action bar to show a dropdown list.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setDisplayShowHomeEnabled(false);

// Set up the dropdown list navigation in the action bar.
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter<String>(actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1, new String[] {
getString(R.string.demo1),
getString(R.string.demo2),
getString(R.string.demo3)}), this);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore the previously serialized current dropdown position.
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}

@Override
public void onSaveInstanceState(Bundle outState) {
// Serialize the current dropdown position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
.getSelectedNavigationIndex());
}

@Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
Fragment fragment = new MainFragmentSection();
Bundle args = new Bundle();
args.putInt(MainFragmentSection.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment).commit();
return true;
}
public static class MainFragmentSection extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public MainFragmentSection() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_fragment,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
}

我想要做的是在同一个操作栏中已存在的下拉菜单旁边添加另一个下拉列表,这样在用户选择时我可以在焦点中重绘当前 fragment (这将取决于第一个)微调器或下拉菜单)进行了一些小的更改,例如对当前焦点 fragment View 中的字符串数组进行排序。

我们将非常感谢一些指导和帮助。

最佳答案

我建议不要这样做,因为操作栏是 Android 中的标准设计模式,对其进行自定义会在应用程序之间造成不一致,从而使用户感到困惑/沮丧。不过我知道有时您会因为各种原因被迫这样做。

因此,您可以做的是为操作栏创建自定义 View 。这使您可以根据自己的需要进行设计。这是一个 vogella 教程,解释了如何操作:

http://www.vogella.com/articles/AndroidActionBar/article.html#menu_customviews

基本上,您可以在 XML 中创建自定义 View ,然后使用代码或主题样式将其添加到操作栏。

希望有帮助。

关于java - 如何在ActionBar中创建二级下拉列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com