私はこれを2日間いじっていますが、まだ理解できないようです.
最終的にすべてのアクティビティでタブホストを表示できるようになりましたが、そのためにはフラグメント アクティビティを作成する必要がありました。これにより、タブボタンのレイアウトが台無しになりました。タブアクティビティを使用していたときは完全に機能しましたが、今では次のようになります。
青いボタンがバー全体を占めるようにします。現在のような背景スペースはありません。レイアウトの問題なのか、スケーリングの問題なのかわかりません。これは、タブを作成するコードと、中央のタブが外側のタブの 2 倍の長さになるようにタブのサイズを変更するコードです。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
// the following code is used to decrease the size of the two end tabs
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
TabHost mth = (TabHost)findViewById(android.R.id.tabhost);
mth.getTabWidget()
.getChildAt(0)
.setLayoutParams(
new LinearLayout.LayoutParams(width / 4,
(int) (width / 4 * .625)));
mth.getTabWidget()
.getChildAt(1)
.setLayoutParams(
new LinearLayout.LayoutParams(width / 2,
(int) (width / 4 * .625)));
mth.getTabWidget()
.getChildAt(2)
.setLayoutParams(
new LinearLayout.LayoutParams(width / 4,
(int) (width / 4 * .625)));
}
/** (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);
}
/**
* Initialise the Tab Host
*/
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
Resources res = getResources();
fragactivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("", res.getDrawable(R.drawable.tab_home)), ( tabInfo = new TabInfo("Tab1",ArrowsActivity.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
fragactivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("", res.getDrawable(R.drawable.tab_search)), ( tabInfo = new TabInfo("Tab2", OptionsActivity.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
fragactivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("", res.getDrawable(R.drawable.twitter)), ( tabInfo = new TabInfo("Tab3", ArrowsActivity.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(fragactivity 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);
}
これは古いコードです (ボタンのサイズが適切な場合)。ビューは、アイコンが親と一致する必要があることを指定した別の .xml ファイルを介して渡されたようです。これを新しいコードで複製しようとしましたが、少し複雑すぎると思います。.xml と共に動作するコードは次のとおりです。
private void setTabs() {
// add tabs
addTab("Home", R.drawable.tab_home, ArrowsActivity.class);
addTab("Attendance", R.drawable.tab_search, OptionsActivity.class);
addTab("@PSUStrength", R.drawable.twitter, OptionsActivity.class);
// addTab("Contact Us", R.drawable.contact, OptionsActivity.class);
}
private void addTab(String labelId, int drawableId, Class<?> c) {
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
// TextView title = (TextView) tabIndicator.findViewById(R.id.title);
// title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
// registerForContextMenu(icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
tab_indicator.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical">
<ImageView android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:src="@drawable/icon"
/>
</RelativeLayout>
これについて何か助けていただければ幸いです。ありがとう。