【发布时间】:2017-04-02 01:06:44
【问题描述】:
如果单击按钮,我有一个 NavigationDrawer 会打开片段。但是现在,我需要在其中一个片段中创建一个 Tabhost。我从未使用过 TabHost,也找不到任何主题来解释如何在片段内的一个 TabHost 中实现 Swipe 效果。在那之前,我已经配置了 Tabhost,但没有任何效果。如果您能解释一下如何操作,我将不胜感激。
PacksFragment.java
package studio.com.archeagemanager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class PacksFragment extends Fragment {
TabHost tabHost;
public PacksFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_packs, container, false);
TabHost host = (TabHost) view.findViewById(R.id.tabhost);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("Tab One");
spec.setContent(R.id.nuia);
spec.setIndicator("NUIA");
host.addTab(spec);
//Tab 2
spec = host.newTabSpec("Tab two");
spec.setContent(R.id.haranya);
spec.setIndicator("HARANYA");
host.addTab(spec);
//Tab 3
spec = host.newTabSpec("Tab three");
spec.setContent(R.id.oceanic);
spec.setIndicator("OCEANIC");
host.addTab(spec);
//Tab 4
spec = host.newTabSpec("Tab four");
spec.setContent(R.id.auroria);
spec.setIndicator("AURORIA");
host.addTab(spec);
for (int i=0; i<4; i++) {
View tabView = host.getTabWidget().getChildTabViewAt(i);
tabView.setPadding(0, 0, 0, 0);
TextView tv = (TextView)tabView.findViewById(android.R.id.title);
tv.setSingleLine();
tv.setTextSize(14);
}
return view;
}
}
【问题讨论】:
标签: java android android-fragments android-tabhost fragment-tab-host