は、私が見てみたかった17Android FragmentTabHost - まだ完全にベークされていませんか?誰もが新しいAndroidのAPIレベルが付属していますFragmentTabHostを使用して、タブのカスタマイズで成功を収めている場合
私はViewPagerのSherlockFragments内巣tabHostことができるように興奮していたが、私はよタブを下に移動する、またはタブのレイアウトを変更するなどの簡単なことをするのに問題があります。
この機能を使用する良い例は誰も見ていますか?
これは私がAndroidのドキュメントで見つけることができる唯一の例であり、その使い方を説明するものはまったくありません。 R.id.fragment1
のレイアウトで定義されているものは無視されているようです。
私の疑問誰かが良いチュートリアルを読んだら、FragmentTabHost か、a)下の方に入れ子のタブを置くか、b)そのタブのレイアウトを変更する方法についてのアイデアがあるとします。
私はすべての通常の方法を試しましたが、XMLレイアウトファイルが上書きされているように見えるので、私はあまり運がありませんでした。
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setContentView(R.layout.fragment_tabs);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
LoaderCustomSupport.AppListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
return mTabHost;
}
いくつかの調査を行った後、サポートライブラリにFragmentTabHostを初期化している不具合があるようです。フラグメントのため
FragmentTabHost.java
private void initFragmentTabHost(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
new int[] { android.R.attr.inflatedId }, 0, 0);
mContainerId = a.getResourceId(0, 0);
a.recycle();
super.setOnTabChangedListener(this);
// If owner hasn't made its own view hierarchy, then as a convenience
// we will construct a standard one here.
/***** HERE COMMENT CODE BECAUSE findViewById(android.R.id.tabs) EVERY TIME IS NULL WE HAVE OWN LAYOUT ******//
// if (findViewById(android.R.id.tabs) == null) {
// LinearLayout ll = new LinearLayout(context);
// ll.setOrientation(LinearLayout.VERTICAL);
// addView(ll, new FrameLayout.LayoutParams(
// ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.FILL_PARENT));
//
// TabWidget tw = new TabWidget(context);
// tw.setId(android.R.id.tabs);
// tw.setOrientation(TabWidget.HORIZONTAL);
// ll.addView(tw, new LinearLayout.LayoutParams(
// ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.WRAP_CONTENT, 0));
//
// FrameLayout fl = new FrameLayout(context);
// fl.setId(android.R.id.tabcontent);
// ll.addView(fl, new LinearLayout.LayoutParams(0, 0, 0));
//
// mRealTabContent = fl = new FrameLayout(context);
// mRealTabContent.setId(mContainerId);
// ll.addView(fl, new LinearLayout.LayoutParams(
// LinearLayout.LayoutParams.FILL_PARENT, 0, 1));
// }
}
XMLレイアウト:ユーザーhere on Google codeはこれに提案を提供してきたと思い
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
私の答えをdownvoteする場合は、議論のためのコメントを残してください! – jamis0n
なぜ、findViewById(android.R.id.tabs)がnullを返すのかを追跡しましたか?非常に一般的な意味では、そのIDが存在し、見つかるはずです... – Kiirani
フラグメント化されていないFragmentActivityで使用すると@ jamis0nが機能しません同じスタックトレースが受信されました:java.lang.IllegalStateException: : –