与水平滚动FragmentTabHost [英] FragmentTabHost with horizontal scroll
问题描述
我想建立一个FragmentTabHost并使其横向滚动。我一直在寻找一个解决方案,但找不到任何东西,所有的帖子都是关于正常TabHost。
I am trying to build a FragmentTabHost and make it horizontal Scrollable. I've been searching for a solution but couldn't find anything, all posts are about normal TabHost.
我使用的是支持库作为<一个在Android网站解释href=\"http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html\">FragmentTabHost
I am using the support library as explained in the android site for FragmentTabHost
我的布局是:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:tag="trip_entry_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<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"/>
</LinearLayout>
已经尝试巢tabwidget成横向滚动视图(这是我能找到其他职位的解决方案,但始终TabHost而不是FragmentTabHost),但没有任何变化:
Already tried to nest the tabwidget into an horizontal scroll view (this is the solution I could find in other posts, but always for TabHost and not for FragmentTabHost), but nothing changes:
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView>
我的标签刚刚收缩,这是真的不很好看。已经有人使它获得一个片段标签主机滚动?
My tabs just got shrinked and that is really not nice looking. Has somebody make it to get a fragment tab host scrollable?
感谢
推荐答案
根据我的经验, FragmentTabHost
不那么在意XML定义,事情一定是编程的。我把它留出工作 HorizontalScrollView
从XML和增加它在我的的onCreate
为 FragmentActivity
。
Based on my experience, the FragmentTabHost
doesn't care much about the xml definitions, things must be made programmatically. I got it working by leaving out the HorizontalScrollView
from the xml and adding it in my onCreate
for the FragmentActivity
.
XML的:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<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="match_parent"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
然后在OnCreate添加标签之后(也编程):
Then in the onCreate after adding the tabs (also programmatically):
TabWidget tw = (TabWidget) findViewById(android.R.id.tabs);
LinearLayout ll = (LinearLayout) tw.getParent();
HorizontalScrollView hs = new HorizontalScrollView(this);
hs.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT));
ll.addView(hs, 0);
ll.removeView(tw);
hs.addView(tw);
hs.setHorizontalScrollBarEnabled(false);
不知道这是最优雅的解决方案,但它似乎工作确定。
Don't know if this is the most elegant solution but it seems to work OK.
这篇关于与水平滚动FragmentTabHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!