我试图得到一个"简单"的UI完成,但我被困在我想把图标在我的底部标签。到目前为止,我得到了以下代码:
` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
@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>
我看了几个不同的教程,但没有一个有一个动作栏和一个带图标的标签栏。我很高兴有任何提示和建议。
提前感谢!
经过几个小时的尝试,我自己找到了答案。
在底部标签栏添加图标的方法是在这里插入"null":
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("AddStuff", getResources().getDrawable(R.drawable.ic_add_white_24dp)),
FragmentTabAdd.class, null);
null必须写在"AddStuff"现在的位置。我甚至用图片给背景上色:
mTabHost.setBackground(getResources().getDrawable(R.drawable.tab_background));
也许这将帮助别人节省一些时间:)