gpt4 book ai didi

java - 将操作栏添加到选项卡式布局

转载 作者:行者123 更新时间:2023-12-01 13:40:45 25 4
gpt4 key购买 nike

我尝试向使用 2.3 版 Android (tabhost) 开发的应用程序添加一个操作栏(通过 appcompat v7 lib,不是导航栏...而是操作栏),但失败了。有人可以帮助我完成这项任务吗?我还尝试使用 fragment (支持 lib v4)更改选项卡 Activity ,但再次没有结果。

我的主要 Activity

public class ProjectActivity extends FragmentActivity implements TabHost.OnTabChangeListener {

private TabHost mTabHost;
private HashMap mapTabInfo = new HashMap();
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.layout_project);
// Step 2: Setup TabHost
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
}

/** (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) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
ProjectActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("tab1").setIndicator("tab1"), ( tabInfo = new TabInfo("tab1", tab1Activity.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
ProjectActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("tab2").setIndicator("tab2"), ( tabInfo = new TabInfo("tab2", tab2Activity.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
ProjectActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("tab3").setIndicator("tab3"), ( tabInfo = new TabInfo("tab3", tab3Activity.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
ProjectActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("tab4").setIndicator("tab4"), ( tabInfo = new TabInfo("tab4", tab4Activity.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(ProjectActivity 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 = (TabInfo) 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();
}
}
}

我的 fragment

public class tab1Activity 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) {
if (container == null) {

return null;
}

return (ScrollView)inflater.inflate(R.layout.layout_dimensions, container, false);
}
}

最佳答案

按照 KickingLettuce 的建议,我使用操作栏 sherlock 解决了这个问题。

关于java - 将操作栏添加到选项卡式布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20809421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com