创建FragmentActivity自定义选项卡 [英] create custom tab in FragmentActivity
问题描述
我试图在Android中创建自定义选项卡上,我有创造的接头插入 FragmentActivity
和 FragmentPagerAdapter
这里是code,但我真的不知道在哪里我使用的充气自定义选项卡
查看tabIndicator = LayoutInflater.from(mContext)。
膨胀(R.layout.tab_item,mTabHost.getTabWidget(),FALSE);
code在FragmentActivity的onCreate方法
mTabHost =(TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
如果(mTabHost!= NULL)的System.out.println(*** ITS NOT NULL ****);
mViewPager =(ViewPager)findViewById(R.id.pager);
mTabsAdapter =新TabsAdapter(这一点,mTabHost,mViewPager);
mTabsAdapter.addTab(mTabHost.newTabSpec(简单)。setIndicator(联系人),
ContactSummaryFragment.class,NULL);
mTabsAdapter.addTab(mTabHost.newTabSpec(定制)。setIndicator(模板),
TextVoiceTemplate.class,NULL);
addTab方法
公共无效addTab(TabHost.TabSpec则tabspec,类<> CLSS,捆绑参数){
tabSpec.setContent(新DummyTabFactory(mContext));
//我得到的地方在这里,我想放置此行
//查看tabIndicator = LayoutInflater.from(mContext).inflate(R.layout.tab_item,mTabHost.getTabWidget(),FALSE);
字符串变量= tabSpec.getTag();
TabInfo信息=新TabInfo(标签,CLSS,参数);
mTabs.add(信息);
mTabHost.addTab(则tabspec);
notifyDataSetChanged();
}
您可以使用则tabspec指定您的布局:
在这里你有addTab在您的适配器
公共无效addTab(TabHost.TabSpec则tabspec,类<> CLSS,捆绑参数)
{
tabSpec.setContent(新DummyTabFactory(mContext));
字符串变量= tabSpec.getTag();
TabInfo信息=新TabInfo(标签,CLSS,参数);
mTabs.add(信息);
mTabHost.addTab(则tabspec);
notifyDataSetChanged();
}
和你填写你的内容(通常游戏中的onCreate)选项卡之前,你可以写你自己的函数
私有静态视图prepareTabView(上下文的背景下,字符串文本,诠释绘制)
{
查看查看= LayoutInflater.from(上下文).inflate(R.layout.tab_layout,NULL);
((TextView中)view.findViewById(R.id.tabTitle))的setText(文本)。
((ImageView的)view.findViewById(R.id.tabImage))setImageResource(绘制);
返回查看;
}
公共静态无效addTab(TabsAdapter适配器,TabHost主机,标题字符串,字符串标签,诠释绘制,类CL)
{
TabHost.TabSpec规格= host.newTabSpec(标签);
查看查看= prepareTabView(host.getContext(),标题,绘制);
spec.setIndicator(视图);
adapter.addTab(规格,CL,NULL);
}
,现在你可以填充你的tabHost用自己的addTab,使用addTab适配器:
mTabHost =(TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
mViewPager =(ViewPager)findViewById(R.id.pager);
mTabsAdapter =新TabsAdapter(这一点,mTabHost,mViewPager,getSupportFragmentManager());
addTab(mTabsAdapter,mTabHost,的getString(R.string.title1),常量,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与绘制,背景,文字等..:
R.layout.tab_layout:
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:重力=顶机器人:背景=@可绘制/ tabbg>
<的TextView
机器人:ID =@ + ID / tabTitle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_marginBottom =30dp
机器人:文本=TextView的
机器人:文字颜色=@色/ tabtextcolor
机器人:TEXTSIZE =12SP机器人:layout_alignParentBottom =真/>
< ImageView的
机器人:ID =@ + ID / tabImage
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =5DP机器人:layout_above =@ ID / tabTitle机器人:layout_centerHorizontal =真/>
< / RelativeLayout的>
这个例子中,你将有水木清华这样的:
i am trying to create custom tab in android , i have create the Tabs into FragmentActivity
and FragmentPagerAdapter
Here is the code of but i really don't know where do i inflate the custom tabs using
View tabIndicator = LayoutInflater.from(mContext).
inflate(R.layout.tab_item, mTabHost.getTabWidget(), false);
code in onCreate method of FragmentActivity
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);
addTab method
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();
}
you can specify your layout with tabSpec:
Here you have addTab in your adapter
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();
}
and before you fill you tab with content (ususally in onCreate) you can write your own function
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);
}
and now you can populate your tabHost with your own addTab, that uses addTab in adapter:
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);
in this example you have R.layout.tab_layout with drawable, background, text and etc... :
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>
with this example you will have smth like this:
这篇关于创建FragmentActivity自定义选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!