是否有可能使用在Android上的操作栏滑动标签和一个微调 [英] Is it possible to use sliding tabs and a spinner in the action bar in Android

查看:138
本文介绍了是否有可能使用在Android上的操作栏滑动标签和一个微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有滑动就可以了标签的活动,我一直在使用ActionBarSherlock,但我想有操作栏中一个微调,以便用户可以选择从纺纱项目及每个项目是一个特定的文件,该文件开辟了值重新填充每个选项卡上的意见。我特地来看看你如何能做到这一点,到目前为止还没有任何运气。我不希望他们进入浏览片段,而是有微调重新填充选项卡上的看法。我看着一个类似的问题<一href="http://stackoverflow.com/questions/11768313/is-it-possible-to-use-dropdown-and-tabs-as-navigation-in-the-action-bar">Is它可以使用下拉菜单和选项卡导航操作栏中的?这是我希望它看起来,但他的问题是使用两种不同类型的导航。请谁能帮助我,让我知道这是可能的。先感谢您。我也只是看着一个Android应用程序,我这是欧洲体育应用程序,并拥有布局是我期待的,如果任何人有一个应用程序,看看我的意思类型。

下面是我的code:

 公共类SlidingTabsActivity扩展SherlockFragmentActivity
{

ViewPager viewPager;
TabsAdapter tabsAdapter;
动作条actionBarTabs;
微调微调;
ArrayAdapter&LT;字符串&GT; arrayAdapter;
LayoutInflater spinnerLayoutInflater;
查看spinnerView;

@覆盖
保护无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);

    viewPager =新ViewPager(本);
    viewPager.setId(R.id.pager);
    的setContentView(viewPager);

    arrayAdapter =新的ArrayAdapter&LT;字符串&GT;(这一点,android.R.layout.simple_spinner_item,R.array.device_description);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerLayoutInflater =(LayoutInflater)this.getBaseContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
    spinnerView = spinnerLayoutInflater.inflate(R.layout.spinner,NULL);
    微调=(微调)spinnerView.findViewById(R.id.tabsSpinner);
    spinner.setAdapter(arrayAdapter);
    spinner.setOnItemSelectedListener(新OnItemSelectedListener()
    {

        @覆盖
        公共无效onItemSelected(适配器视图&LT;&GT;为arg0,查看ARG1,
                INT ARG2,长ARG3)
        {
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onNothingSelected(适配器视图&LT;&GT;为arg0)
        {
            // TODO自动生成方法存根

        }

    });

    actionBarTabs = getSupportActionBar();
    actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBarTabs.setCustomView(spinnerView);
    actionBarTabs.setDisplayShowCustomEnabled(真正的);
    actionBarTabs.setDisplayHomeAsUpEnabled(真正的);

    tabsAdapter =新TabsAdapter(这一点,viewPager); //声明的观点寻呼机查看标签适配器类

    / *添加片段选项卡适配器* /
    tabsAdapter.addTab(actionBarTabs.newTab()的setText(PV),Fragment_1.class,空);
    tabsAdapter.addTab(actionBarTabs.newTab()的setText(CONFIG),Fragment_2.class,空);
    tabsAdapter.addTab(actionBarTabs.newTab()的setText(DIAG),Fragment_3.class,空);

}
 

下面是我的微调code:

 &LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;

&LT;微调
    机器人:ID =@ + ID / tabsSpinner
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真/&GT;

&LT; / RelativeLayout的&GT;
 

我得到了logcat的这条消息时,pressing的微调:

 十二月11日至26日:22:44.282:W / InputEventReceiver(7217):试图完成一个输入事件,但输入的事件接收器已经被释放。
12月11号至26号:22:44.282:W / InputMethodManagerService(525):窗口已经集中,忽略了聚焦增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@4216a290属性= NULL,记号= android.os。 BinderProxy @ 42618330
 

解决方案

因此​​,首先创建一个标记文件,将您的微调,例如:

 &LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =match_parent&GT;

    &LT;微调
        机器人:ID =@ + ID /微调
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:spinnerMode =下拉列表中&GT;
    &LT; /微调&GT;

&LT; / RelativeLayout的&GT;
 

接下来创建一个菜单资源,例如 main_menu.xml 并把它 RES /菜单文件夹:

 &LT;菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;

    &LT;项目
        机器人:ID =@ + ID / action_change_content
        机器人:actionLayout =@布局/ spinner_layout
        机器人:orderInCategory =100
        机器人:showAsAction =总是
        机器人:标题=@字符串/空&GT;
    &LT; /项目&GT;

&LT; /菜单&gt;
 

付费,我们指定该菜单项自定义操作布局你的注意力 - 这是我们前面创建的布局,它包含了我们的微调。接下来,在你的活动(或片段),您必须抬高这个菜单:
使用这个,如果你在活动

创建这个菜单

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    。getMenuInflater()膨胀(R.menu.main_menu,菜单);
    ... //初始化喷丝,下面将说明
    返回true;
}
 

或者,如果你在片段创建菜单:

  @覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
    menu.clear();
    inflater.inflate(R.menu.card_storage,菜单);
    ... //初始化微调
}
 

如果您在创建这个菜单片段不要忘了叫 setHasOptionMenu(真)介于 Fragment.onCreate()

将这个代替 ... //初始化微调

 菜单项项= menu.findItem(R.id.action_change_content); //标识我们的自定义菜单项
CustomSpinnerAdapter适配器=新CustomSpinnerAdapter(); //您的适配器
微调微调=(微调)item.getActionView()findViewById(R.id.spinner)。
spinner.setAdapter(适配器);
spinner.setOnItemSelectedListener(本);
 

我希望我没有错过了一些东西。祝你好运!

I currently have an Activity with sliding tabs on it and I have been using ActionBarSherlock, but I wanted to have a spinner in the action bar, so that the user could select items from the spinner and each item is a specific file which opens up values to re-populate the views on each tab. I have looked to see how you can do this and so far have not had any luck. I don't want them to both navigate through fragments, but instead have the spinner to re-populate views on the tabs. I looked at a similar question Is it possible to use dropdown AND tabs as navigation in the action bar? which is how I want it to look, but his question was to use two different types of navigation. Please can anyone help me and let me know if this is possible. Thank you in advance. I have also just looked at an Android application that I have which is the Eurosport app and that has the type of layout that I am looking for if anyone has that application to see what I mean.

Here is my code:

public class SlidingTabsActivity extends SherlockFragmentActivity
{

ViewPager viewPager;
TabsAdapter tabsAdapter;
ActionBar actionBarTabs;
Spinner spinner;
ArrayAdapter<String> arrayAdapter;
LayoutInflater spinnerLayoutInflater;
View spinnerView;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    viewPager = new ViewPager(this);
    viewPager.setId(R.id.pager);
    setContentView(viewPager);

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.device_description);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerLayoutInflater = (LayoutInflater) this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    spinnerView = spinnerLayoutInflater.inflate(R.layout.spinner, null);
    spinner = (Spinner) spinnerView.findViewById(R.id.tabsSpinner);
    spinner.setAdapter(arrayAdapter);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener()
    {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) 
        {
            // TODO Auto-generated method stub

        }

    });

    actionBarTabs = getSupportActionBar();
    actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBarTabs.setCustomView(spinnerView);
    actionBarTabs.setDisplayShowCustomEnabled(true);
    actionBarTabs.setDisplayHomeAsUpEnabled(true);

    tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view

    /* Adds fragments to the tabs adapter */
    tabsAdapter.addTab(actionBarTabs.newTab().setText("PV"), Fragment_1.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("CONFIG"), Fragment_2.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("DIAG"), Fragment_3.class, null);

}

Here is my Spinner code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<Spinner
    android:id="@+id/tabsSpinner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

</RelativeLayout>

I get this message in the logcat when pressing the Spinner:

11-26 12:22:44.282: W/InputEventReceiver(7217): Attempted to finish an input event but the input event receiver has already been disposed.
11-26 12:22:44.282: W/InputMethodManagerService(525): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4216a290 attribute=null, token = android.os.BinderProxy@42618330

解决方案

So, first of all create a markup file to place your Spinner, e.g:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:spinnerMode="dropdown" >
    </Spinner>

</RelativeLayout>  

Next create a menu resource, e.g. main_menu.xml and put it to res/menu folder:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_change_content"
        android:actionLayout="@layout/spinner_layout"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/empty">
    </item>

</menu>  

Paid your attention that we specified custom action layout for this menu item - this is a layout we have created earlier and it contains our Spinner. Next in your Activity(or Fragment) you must inflate this menu:
Use this if you creating this menu in Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    ...// init spinner, will be explained below
    return true;
}

Or if you creating menu in Fragment:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.card_storage, menu);
    ...// init spinner
}  

If you creating this menu for Fragment don't forget to call setHasOptionMenu(true) somewhere in Fragment.onCreate().

Put this instead of ... // init spinner

MenuItem item = menu.findItem(R.id.action_change_content); // id of our custom menu item
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(); // your adapter
Spinner spinner = (Spinner) item.getActionView().findViewById(R.id.spinner); 
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);  

I hope I didn't missed out something. Good luck!

这篇关于是否有可能使用在Android上的操作栏滑动标签和一个微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆