gpt4 book ai didi

android - 在 FragmentActivity 中创建自定义选项卡

转载 作者:搜寻专家 更新时间:2023-11-01 09:08:43 26 4
gpt4 key购买 nike

我正在尝试在 android 中创建自定义选项卡,我已将选项卡创建到 FragmentActivityFragmentPagerAdapter

这是代码,但我真的不知道在哪里使用

来扩展自定义选项卡
View tabIndicator = LayoutInflater.from(mContext).
inflate(R.layout.tab_item, mTabHost.getTabWidget(), false);

FragmentActivity的onCreate方法中的代码

        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();

if(mTabHost!=null) System.out.println("***ITS NOT NULL****");

mViewPager = (ViewPager)findViewById(R.id.pager);

mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

mTabsAdapter.addTab(mTabHost.newTabSpec("simple").setIndicator("Contacts"),
ContactSummaryFragment.class, null);

mTabsAdapter.addTab(mTabHost.newTabSpec("custom").setIndicator("Template"),
TextVoiceTemplate.class, null);

添加标签的方法

  public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mContext));

// I got to place this line somewhere here i guess
//View tabIndicator = LayoutInflater.from(mContext).inflate(R.layout.tab_item, mTabHost.getTabWidget(), false);
String tag = tabSpec.getTag();

TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}

最佳答案

您可以使用 tabSpec 指定您的布局:

这里你的适配器中有 addTab

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
{
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();

TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}

在用内容填充选项卡之前(通常在 onCreate 中),您可以编写自己的函数

private static View prepareTabView(Context context, String text, int drawable)
{
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
((TextView) view.findViewById(R.id.tabTitle)).setText(text);
((ImageView) view.findViewById(R.id.tabImage)).setImageResource(drawable);
return view;

}

public static void addTab(TabsAdapter adapter, TabHost host, String title, String tag, int drawable, Class cl)
{
TabHost.TabSpec spec = host.newTabSpec(tag);
View view = prepareTabView(host.getContext(), title, drawable);
spec.setIndicator(view);

adapter.addTab(spec, cl,null);

}

现在您可以使用自己的 addTab 填充 tabHost,它在适配器中使用 addTab:

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();

mViewPager = (ViewPager) findViewById(R.id.pager);

mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager, getSupportFragmentManager());

addTab(mTabsAdapter, mTabHost, getString(R.string.title1), CONST, R.drawable.tab_drawable1, Fragment1.class);
addTab(mTabsAdapter, mTabHost, getString(R.string.title1), CONST2, R.drawable.tab_formula_image_selector, Fragment2.class);

在这个例子中你有 R.layout.tab_layout 和 drawable, background, text 等等...:

R.layout.tab_layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top" android:background="@drawable/tabbg">

<TextView
android:id="@+id/tabTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:text="TextView"
android:textColor="@color/tabtextcolor"
android:textSize="12sp" android:layout_alignParentBottom="true"/>

<ImageView
android:id="@+id/tabImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp" android:layout_above="@id/tabTitle" android:layout_centerHorizontal="true"/>

</RelativeLayout>

在这个例子中你会得到这样的东西:

Sample result

关于android - 在 FragmentActivity 中创建自定义选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9958303/

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