gpt4 book ai didi

android - 如何在 fragment 中设置 FragmentTabHost 的样式

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:55:02 26 4
gpt4 key购买 nike

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

>

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

/>

</LinearLayout>

这是托管 fragment 。

public class TabPractice extends Fragment{
private FragmentTabHost mTabHost;

//Mandatory Constructor
public TabPractice() {
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);




}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.tabs_practice,container, false);


mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

mTabHost.addTab(mTabHost.newTabSpec("Practice All").setIndicator("Practice All"),
WelcomeScreen.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Practive Fav").setIndicator("Practive Fav"),
FavoriteList.class, null);



return rootView;

}

enter image description here

这就是我的标签的外观,我不知道如何设置这些标签的样式。在大多数解释样式的示例中,有一个 XML 格式的 TabWidget,当我尝试在我的布局中添加它时它不起作用。

1) 有人可以帮助我理解在 Fragment 中托管 FragmenttabHost(mycode- 来自一些教程)与在 FragmentActivity 中托管 FragmenttabHost 有何不同??

2) 如何在我的代码中设置选项卡的样式??

谢谢。

最佳答案

按如下方式制作您的 xml:

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

<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >

<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:layout_weight="0"
android:orientation="horizontal" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/dark_blue" />

<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"
android:background="@android:color/white" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>

TabPractice Fragment 中的选项卡代码如下:

mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);

mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

View tabIndicatorToday = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorToday.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.today));

mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.today)).setIndicator(tabIndicatorToday), EntriesTodayFragment.class, null);

View tabIndicatorLive = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorLive.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.live));

和drawables中的tab_indicator xml如下:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@color/white"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_tab_txt"
style="?android:attr/tabWidgetStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/live"
android:textColor="@drawable/tab_text_color"
android:textStyle="bold" />

<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_gravity="bottom"
android:background="@drawable/tab_underline_selector" />
</LinearLayout>

<View
android:layout_width="1dp"
android:id="@+id/tabs_divider_view"
android:layout_height="30dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/grey_background" />

</LinearLayout>

现在您可以根据需要自定义选项卡背景、突出显示颜色和选项卡下方的线条。希望对您有所帮助。

关于android - 如何在 fragment 中设置 FragmentTabHost 的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25389630/

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