2016-06-25 55 views
0

我试图让一个“简单”的用户界面完成,但我被卡在了要将图标放在底部选项卡中的位置。到目前为止,我得到了下面的代码:在操作栏处于活动状态时将图标添加到标签

'公共类MainActivity扩展ActionBarActivity {

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

    setContentView(R.layout.activity_main); 
    FragmentTabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 

    mTabHost.addTab(
      mTabHost.newTabSpec("tab1").setIndicator("AddStuff", getResources().getDrawable(R.drawable.ic_add_white_24dp)), 
      FragmentTabAdd.class, null); 
    mTabHost.addTab(
      mTabHost.newTabSpec("tab2").setIndicator("Favorites", getResources().getDrawable(R.drawable.ic_add_white_24dp)), 
      FragmentTabSelectFavorites.class, null); 
    mTabHost.addTab(
      mTabHost.newTabSpec("tab3").setIndicator("Messages", getResources().getDrawable(R.drawable.ic_add_white_24dp)), 
      FragmentTabMessages.class, null); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
}` 

,我得到了我的.XML为每个标签下面:

<?xml version="1.0" encoding="utf-8"?> <selector android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected, use black --> <item android:drawable="@drawable/ic_add_black_24dp" android:state_selected="true" /> <!-- When not selected, use white--> <item android:drawable="@drawable/ic_add_white_24dp" /> </selector>

我的工作通过几个不同的教程,但没有一个具有图标的Actionbar和Tabbar。我很高兴有任何提示和建议。

预先感谢您!

回答

0

我在几个小时尝试不同的事情后自己找到了答案。

将图标添加至底部标签栏的方法是插入“空”在这里:

mTabHost.addTab(
     mTabHost.newTabSpec("tab1").setIndicator("AddStuff", getResources().getDrawable(R.drawable.ic_add_white_24dp)), 
     FragmentTabAdd.class, null); 

空已被写入其中“AddStuff”就是现在。 我甚至带有彩色图像的背景:

mTabHost.setBackground(getResources().getDrawable(R.drawable.tab_background)); 

也许这将帮助别人节省一些时间:)

相关问题