Java 使用片段和TabHost android
我有这个代码,它在我的应用程序的按钮上创建了一个标签。我创建了3个标签。“我的选项卡”的实现几乎相同,仅更改布局的颜色。我想测试,但它没有显示我创建的布局 也许我做错了什么 这是我的第一个选项卡类:Java 使用片段和TabHost android,java,android,android-fragments,Java,Android,Android Fragments,我有这个代码,它在我的应用程序的按钮上创建了一个标签。我创建了3个标签。“我的选项卡”的实现几乎相同,仅更改布局的颜色。我想测试,但它没有显示我创建的布局 也许我做错了什么 这是我的第一个选项卡类: public class Tab1Fragment extends Fragment { /** (non-Javadoc) * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflat
public class Tab1Fragment extends Fragment {
/** (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Log.v("test","onCreateView:Tab1Fragment");
if (container == null) {
return null;
}
LinearLayout theLayout = (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false);
// Register for the Button.OnClick event
Button b = (Button)theLayout.findViewById(R.id.frag1_button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Tab1Fragment.this.getActivity(), "OnClickMe button clicked", Toast.LENGTH_LONG).show();
}
});
return theLayout;
}
}
这个班级的布局是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF0000" >
<Button
android:id="@+id/frag1_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
但是,当我运行此命令时,会为我调用并打印此命令:
onCreateView:Tab1Fragment
但我没有看到我的按钮和红色布局
我发现我的错误现在标签和框架显示在设备2.1和大小480x800,但在平板电脑上它不工作一些如何
以下是如何添加选项卡的代码:
包my.test.project
import java.util.HashMap;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
/**
* @author mwho
*
*/
public class TabsFragmentActivity extends FragmentActivity implements
TabHost.OnTabChangeListener {
private TabHost mTabHost;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabInfo>();
private TabInfo mLastTab = null;
private class TabInfo {
private String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class<?> clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
class TabFactory implements TabContentFactory {
private final Context mContext;
/**
* @param context
*/
public TabFactory(Context context) {
mContext = context;
}
/**
* (non-Javadoc)
*
* @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
/**
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Step 1: Inflate layout
setContentView(R.layout.main_tab_activity);
// Step 2: Setup TabHost
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
Log.v("test","tabs");
}
/**
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
// selected
super.onSaveInstanceState(outState);
}
/**
* Step 2: Setup TabHost
*/
private void initialiseTabHost(Bundle args) {
//ge
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab1").setIndicator("Tab 1"),
(tabInfo = new TabInfo("Tab1", Tab1Fragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab2").setIndicator("Tab 2"),
(tabInfo = new TabInfo("Tab2", Tab2Fragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab3").setIndicator("Tab 3"),
(tabInfo = new TabInfo("Tab3", Tab3Fragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
// Default to first tab
this.onTabChanged("Tab1");
//
mTabHost.setOnTabChangedListener(this);
}
/**
* @param activity
* @param tabHost
* @param tabSpec
* @param clss
* @param args
*/
private static void addTab(TabsFragmentActivity activity, TabHost tabHost,TabHost.TabSpec tabSpec, TabInfo tabInfo) {
// Attach a Tab view factory to the spec
tabSpec.setContent(activity.new TabFactory(activity));
String tag = tabSpec.getTag();
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.detach(tabInfo.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
tabHost.addTab(tabSpec);
}
/**
* (non-Javadoc)
*
* @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
*/
public void onTabChanged(String tag) {
TabInfo newTab = this.mapTabInfo.get(tag);
if (mLastTab != newTab) {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this,newTab.clss.getName(), newTab.args);
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
}
}
<?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" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
</LinearLayout>
</TabHost>
</LinearLayout>
import java.util.HashMap;
导入android.content.Context;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.app.FragmentTransaction;
导入android.util.Log;
导入android.view.view;
导入android.widget.TabHost;
导入android.widget.TabHost.TabContentFactory;
/**
*@作者mwho
*
*/
公共类选项卡FragmentActivity扩展了FragmentActivity实现
TabHost.OnTabChangeListener{
私有TabHost-mTabHost;
私有HashMap mapTabInfo=新HashMap();
private TabInfo mLastTab=null;
私有类TabInfo{
私有字符串标签;
私家级CLS;
私有包args;
私有片段;
TabInfo(字符串标记、类clazz、包参数){
this.tag=tag;
this.clss=clazz;
this.args=args;
}
}
类TabFactory实现TabContentFactory{
私有最终上下文mContext;
/**
*@param上下文
*/
公共选项卡工厂(上下文){
mContext=上下文;
}
/**
*(非Javadoc)
*
*@see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
公共视图createTabContent(字符串标记){
视图v=新视图(mContext);
v、 设置最小宽度(0);
v、 设置最小高度(0);
返回v;
}
}
/**
*(非Javadoc)
*
*@see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//步骤1:充气布局
setContentView(R.layout.main_选项卡_活动);
//步骤2:设置TabHost
initialiseTabHost(savedInstanceState);
如果(savedInstanceState!=null){
mTabHost.setCurrentTabByTag(savedInstanceState.getString(“tab”));
}
Log.v(“测试”、“标签”);
}
/**
*(非Javadoc)
*
*@see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/
SaveInstanceState上受保护的无效(束超出状态){
outState.putString(“tab”,mTabHost.getCurrentTabTag());//保存选项卡
//精选
super.onSaveInstanceState(超出状态);
}
/**
*步骤2:设置TabHost
*/
私有void initialiseTabHost(Bundle args){
//通用电气
mTabHost=(TabHost)findViewById(android.R.id.TabHost);
mTabHost.setup();
TabInfo TabInfo=null;
TabsFragmentActivity.addTab(this,this.mTabHost,this.mTabHost
.newTabSpec(“表1”).setIndicator(“表1”),
(tabInfo=newtabinfo(“Tab1”,Tab1Fragment.class,args));
this.mapTabInfo.put(tabInfo.tag,tabInfo);
TabsFragmentActivity.addTab(this,this.mTabHost,this.mTabHost
.newTabSpec(“Tab2”).setIndicator(“Tab2”),
(tabInfo=newtabinfo(“Tab2”,Tab2Fragment.class,args));
this.mapTabInfo.put(tabInfo.tag,tabInfo);
TabsFragmentActivity.addTab(this,this.mTabHost,this.mTabHost
.newTabSpec(“表3”).setIndicator(“表3”),
(tabInfo=newtabinfo(“Tab3”,Tab3Fragment.class,args));
this.mapTabInfo.put(tabInfo.tag,tabInfo);
//默认设置为第一个选项卡
本表已更改(“表1”);
//
mTabHost.setOnTabChangedListener(此);
}
/**
*@param活动
*@param tabHost
*@param tabSpec
*@param-clss
*@param args
*/
私有静态void addTab(TabsFragmentActivity活动,TabHost TabHost,TabHost.TabSpec TabSpec,TabInfo TabInfo){
//将选项卡视图工厂附着到等级库
tabSpec.setContent(activity.new TabFactory(activity));
String tag=tabSpec.getTag();
//检查一下,看看我们是否已经有了这个标签的一个片段,可能吧
//从以前保存的状态。如果是,请将其停用,因为
//初始状态是不显示选项卡。
tabInfo.fragment=activity.getSupportFragmentManager().findFragmentByTag(标记);
if(tabInfo.fragment!=null&&!tabInfo.fragment.isDetached()){
FragmentTransaction ft=activity.getSupportFragmentManager().beginTransaction();
ft.detach(tabInfo.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
tabHost.addTab(tabSpec);
}
/**
*(非Javadoc)
*
*@see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
*/
已更改的公用void onTabChanged(字符串标记){
TabInfo newTab=this.mapTabInfo.get(标记);
如果(mLastTab!=newTab){
FragmentTransaction ft=this.getSupportFragmentManager().beginTransaction();
如果(mLastTab!=null){
if(mLastTab.fragment!=null){
ft.分离(mLastTab.碎片);
}
}
if(newTab!=null){
if(newTab.fragment==null){