gpt4 book ai didi

android - 自定义 FragmentTabHost 在底部设置 TabWidget

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:54 24 4
gpt4 key购买 nike

目前我正在尝试为我的项目实现 FragmentTabHost。我对这个 fragment 还很陌生,但我发现它在重用布局等方面非常棒,这就是为什么我想进一步插入自己。现在,我阅读了有关如何使用 fragment 创建选项卡的教程,并阅读了本教程:

http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/

现在一切正常,只是 tabWidget 位于我的布局顶部,而我希望它位于底部。我发现我需要在初始化所有选项卡后设置 tabWidget,所以我尝试添加这些代码:

mTabWidget = (TabWidget) findViewById(android.R.id.tabs);

mTabWidget.setBackgroundColor(Color.WHITE);
mTabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
mTabWidget.setGravity(Gravity.BOTTOM);

现在这个已经消除了分隔线并改变了颜色,但显然不会将我的小部件放在布局的底部。现在我该怎么做?

我还尝试编辑 Tabhost xml 并将 TabWidget 放在 FrameLayout 之后,但没有任何反应。这是 xml:

    <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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
android:id="@+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
/>

</LinearLayout>

</android.support.v4.app.FragmentTabHost>

最佳答案

我已引用此链接 github example

这将是您的布局:

<?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" >

<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />

<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>

</LinearLayout>

对于您的自定义标签:

mTabHost.addTab(setIndicator(mTabHost.newTabSpec("Tab1"),
R.drawable.image1),

public TabSpec setIndicator(Context ctx,TabSpec spec, int resid) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(ctx).inflate(R.layout.tabs_text, null);
v.setBackgroundResource(resid);
TextView text = (TextView) v.findViewById(R.id.tab_title);
text.setText(spec.getTag());
return spec.setIndicator(v);
}

编辑

 //To set drawable to your perticular TAB 
mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_login);

结束编辑

如果要添加drawable(选择器):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/tab_compose_h" android:state_selected="true"/>
<item android:drawable="@drawable/tab_compose_h" android:state_pressed="true"/>
<item android:drawable="@drawable/tab_compose"/>

</selector>

关于android - 自定义 FragmentTabHost 在底部设置 TabWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18527071/

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