我有来自this example的导航抽屉..现在当我运行应用程序时,第一个抽屉项目正在出现
在我的片段中我添加了Tabhost,在我的Tabhost中我可以设置文本,但我想放置图像而不是文本
以下是我的代码可以任何一个帮助
public class HomeFragment extends Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
private FragmentTabHost tabHost;
private SearchView searchView;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
// tabHost.newTabSpec("All").setIndicator(null,getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab_template_two_tabs_all);
// tabHost.addTab(tabHost.newTabSpec("one").setIndicator("The Tab", getResources().getDrawable(R.drawable.ic_launcher)).setContent(new Intent(getActivity(), DiscoverFragment.class)));
tabHost.addTab(tabHost.newTabSpec("First").setIndicator("First"), DiscoverFragment.class, arg1);
// tabHost.addTab(tabHost.newTabSpec("TAb1").setIndicator("Tab1"),FragmentA.class,arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
tabHost.addTab(tabHost.newTabSpec("Sec").setIndicator("Sec"), ShopFragment.class, arg2);
Bundle arg3 = new Bundle();
arg2.putInt("Arg for Frag3", 3);
tabHost.addTab(tabHost.newTabSpec("Third").setIndicator("Third"), Thirdtab.class, arg3);
Bundle arg4 = new Bundle();
arg2.putInt("Arg for Frag4", 4);
tabHost.addTab(tabHost.newTabSpec("Four").setIndicator("Four"), FourthTabs.class, arg4);
return tabHost;
}
}
XML
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
答案 0 :(得分:2)
您还可以使用TabActivity来使用Activity。 TabActivity已弃用。
TabHost tabHost;
tabHost = getTabHost();
setTabs();
String arrTabId[] = new String[] { "tab 1", "tab 2",
"tab 3", "tab 4", "tab 5" };
private void setTabs() {
addTab(arrTabId[0], R.drawable.first_tab, Activity_List.class);
addTab(arrTabId[1], R.drawable.second_tab, PedidosActivity.class);
addTab(arrTabId[2], R.drawable.pedidos, ApplicationActivity.class);
addTab(arrTabId[3], R.drawable.third_tab, ChatActivity.class);
addTab(arrTabId[4], R.drawable.fourth_tab, SettingActivity.class);
}
private void addTab(String labelId, int drawableId, Class<?> c) {
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec(labelId);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
在设计库中,您还可以获得滑动标签。您也可以通过膨胀布局来自定义它。
答案 1 :(得分:1)
spec = tabHost.newTabSpec("First").setIndicator("name",getResources().getDrawable(R.drawable.port))
.setContent(intent);
试试吧。
答案 2 :(得分:1)
将您的tabhost修改为
tabHost.addTab(tabHost.newTabSpec("Sec").
setIndicator(getTabIndicator(tabHost.getContext(), R.string.tab1, R.drawable.image1)), ShopFragment.class, arg2);
/** For both Image and text view
* Remove text if you don't need
*/
private View getTabIndicator(Context context, int title, int icon) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
ImageView iv = (ImageView) view.findViewById(R.id.indicatorImageView);
iv.setImageResource(icon);
TextView tv = (TextView) view.findViewById(R.id.indicatorText);
tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
tv.setText(title);
return view;
}
tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView android:id="@+id/indicatorImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/indicatorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" />
</LinearLayout>