动作按钮显示为溢出

时间:2014-09-19 10:14:32

标签: java android android-actionbar android-tabs

我已将操作标签操作按钮添加到操作栏

这是我想要完成的事情 -

target

但这就是我所拥有的 -

what I have

请注意底部的操作栏。即使添加XXX:showAsAction="always"

,我添加的操作按钮也会显示为溢出

这是我的活动代码 -

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener  {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabs = { "Top Rated", "Games", "Movies" };

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

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);       

        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        getActionBar().setDisplayShowHomeEnabled(false);              
        getActionBar().setDisplayShowTitleEnabled(false);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.lol, menu);
    return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

菜单文件 -

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:TOB="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/set"
          android:icon="@drawable/ic_action_settings"
          android:title="lol"  
          TOB:showAsAction="always"/>

    <item android:id="@+id/back"
          android:icon="@drawable/ic_action_back"
          android:title="bac"  
          TOB:showAsAction="always"/>

    <item android:id="@+id/car"
          android:icon="@drawable/ic_action_previous_item"
          android:title="car"  
          TOB:showAsAction="always"/>

</menu>

我的Mainfest中还添加了android:uiOptions="splitActionBarWhenNarrow"

1 个答案:

答案 0 :(得分:1)

首先创建你的action_tab.xml(用于标签布局,你可以使用按钮)和custom_bar.xml,如下所示。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:orientation="horizontal"
android:paddingEnd="8dip" >

<ImageView
android:id="@+id/myImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/about"
android:src="@drawable/ic_action_about" />

<ImageView
android:id="@+id/myImageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/help"
android:src="@drawable/ic_action_download" />

<ImageView
android:id="@+id/myImageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/settings"
android:src="@drawable/ic_action_settings" />

然后在主要活动中添加以下代码。

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

ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

View view = View.inflate(getApplicationContext(), R.layout.actionbar,
        null); 
actionBar.setCustomView(view);
} 

对于底栏,请对custom_bar.xml进行充气:

@Override 
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.browser_main, menu);
RelativeLayout relativeLayout = (RelativeLayout) menu.findItem(R.id.layout_item).getActionView();

View inflatedView = getLayoutInflater().inflate(
        R.layout.custom_bar, null);

relativeLayout.addView(inflatedView);

return true; 
} 

你必须进行一些研究以显示标签(如何使其滑动),你可以拥有你想要的精确外观。