对于FragmentTabHost使用方法详解感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解host_framerate,并且为您提供关于Android1.6和Fragment&Tabho
对于FragmentTabHost使用方法详解感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解host_framerate,并且为您提供关于Android 1.6和Fragment&Tabhost、Android Fragment FragmentTabHost 问题、Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)、android fragmenttabhost 如何让tabs位于底部(个人笔记)的宝贵知识。
本文目录一览:- FragmentTabHost使用方法详解(host_framerate)
- Android 1.6和Fragment&Tabhost
- Android Fragment FragmentTabHost 问题
- Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)
- android fragmenttabhost 如何让tabs位于底部(个人笔记)
FragmentTabHost使用方法详解(host_framerate)
FragmentTabHost是support-v包下提供的用于集成和管理Fragment页面的组件.
今天要实现的效果图如下:
整体结构是MainActivity+5个模块的Fragment.
MainActivity的布局如下:
<?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"> <!--真正的内容视图,用于展示Fragment--> <FrameLayout android:id="@+id/real_tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <!--tabhost,必须使用系统的id--> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" > <!--tabcontent,必须使用系统的id--> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/> </android.support.v4.app.FragmentTabHost> </LinearLayout>
每个tab的布局如下:
<?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="wrap_content" android:gravity="center" android:orientation="vertical"> <!--tab图片--> <ImageView android:id="@+id/iv_tab" android:layout_width="26dp" android:layout_height="26dp" /> <!--tab名字--> <TextView android:id="@+id/tv_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="1dp" android:textSize="12sp"/> </LinearLayout>
MainActivity代码如下:
package blog.csdn.net.mchenys.bsbdj.modul.main; import android.content.res.ColorStateList; import android.os.Bundle; import android.support.v4.app.FragmentTabHost; import android.text.TextUtils; import android.view.View; import android.widget.ImageView; import android.widget.TabHost; import android.widget.TextView; import blog.csdn.net.mchenys.bsbdj.R; import blog.csdn.net.mchenys.bsbdj.common.base.BaseActivity; import blog.csdn.net.mchenys.bsbdj.modul.attention.view.AttentionFragment; import blog.csdn.net.mchenys.bsbdj.modul.essence.view.EssenceFragment; import blog.csdn.net.mchenys.bsbdj.modul.mine.view.mineFragment; import blog.csdn.net.mchenys.bsbdj.modul.newpost.view.NewpostFragment; import blog.csdn.net.mchenys.bsbdj.modul.publish.view.PublishFragment; import blog.csdn.net.mchenys.bsbdj.mvp.presenter.impl.MvpBasePresenter; /** * Created by mChenys on 2016/5/27. */ public class MainActivity extends BaseActivity { //定义数组来存放tab的图片选择器 private int[] mTabImage = {R.drawable.main_bottom_essence_selector,R.drawable.main_bottom_latest_selector,R.drawable.main_bottom_writeposts_selector,R.drawable.main_bottom_news_selector,R.drawable.main_bottom_my_selector}; //tab选项卡的文字 private String[] mTabTitle = {"精华","新帖","","关注","我的"}; //每个tab对应的Fragment的字节码对象 private Class[] fragmentArray = {EssenceFragment.class,NewpostFragment.class,PublishFragment.class,AttentionFragment.class,mineFragment.class}; @Override protected boolean isHomePage() { return true; } @Override public Integer getLayoutResId() { return R.layout.activity_main; } @Override public void initView() { //获取tabhost FragmentTabHost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); //绑定tabContent tabHost.setup(this,getSupportFragmentManager(),R.id.real_tabcontent); //去掉分割线 tabHost.getTabWidget().setDividerDrawable(null); for (int i = 0; i < fragmentArray.length; i++) { //绑定Fragment,添加到的FragmentTabHost //设置tab的名称和view TabHost.TabSpec tabSpec = tabHost. newTabSpec(mTabTitle[i]). setIndicator(getTabItemView(i)); Bundle bundle = new Bundle(); bundle.putString("title",mTabTitle[i]); //添加tab和关联对应的fragment tabHost.addTab(tabSpec,fragmentArray[i],bundle); //设置tab的背景色 tabHost.getTabWidget(). getChildAt(i). setBackgroundColor(getResources().getColor(R.color.bgColor)); } //默认选中第一个tab tabHost.setCurrentTab(0); //设置tab的切换监听 tabHost.setonTabChangedListener(new TabHost.OnTabchangelistener() { @Override public void onTabChanged(String tabId) { //可以在这里监听tab的切换 } }); } //tab的字体选择器 ColorStateList mColorStateList; /** * 给Tab按钮设置图标和文字 */ private View getTabItemView(int index) { View view = getLayoutInflater().inflate(R.layout.view_tab_indicator,null); ImageView imageView = (ImageView) view.findViewById(R.id.iv_tab); TextView textView = (TextView) view.findViewById(R.id.tv_tab); //设置图片选择器 imageView.setimageResource(mTabImage[index]); //设置字体选择器 if (mColorStateList == null) { mColorStateList = getResources(). getColorStateList(R.color.main_bottom_text_selector); textView.setTextColor(mColorStateList); } //设置tab的文字 if (TextUtils.isEmpty(mTabTitle[index])) { //如果没有名称,则隐藏tab下的textView textView.setVisibility(View.GONE); } else { textView.setVisibility(View.VISIBLE); textView.setText(mTabTitle[index]); } return view; } @Override public void initListener() { } @Override public void initData() { } @Override public void reLoadData() { } @Override public void onClick(View v) { } @Override public MvpBasePresenter bindPresenter() { return null; } }
最后附上字体选择器
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="false" android:color="@color/main_bottom_text_normal" /> <item android:state_selected="true" android:color="@color/main_bottom_text_select" /> </selector>
图片选择器有5个,这里附上一个,其他类似:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="false" android:drawable="@drawable/main_bottom_essence_normal" /> <item android:state_selected="true" android:drawable="@drawable/main_bottom_essence_press" /> </selector>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
Android 1.6和Fragment&Tabhost
我正在升级Android应用程序(1.6兼容性),该应用程序使用TabHost
显示三个带有嵌套活动的不同选项卡。
当时,我使用ActivityGroup
技巧在选项卡中显示嵌套的活动,但是我对此方法感到非常不满意,因为处理某些功能确实很麻烦。
我听说过1.6版的Fragments
API兼容性软件包,并且Fragment
看起来非常适合我想做的事情(在带有过渡效果和内容的选项卡中显示嵌套视图/功能),但我无法使其与TabHost
(这是为了与一起使用,Action
Bar
但在兼容性包中不可用)。
你们有没有找到一种在您的应用程序中创建这种结构的方法?
我的错误是:
ERROR/AndroidRuntime(955): Caused by: java.lang.RuntimeException: Unable
开始活动ComponentInfo
{com.XXX}:java.lang.IllegalArgumentException:找不到片段MyFragment的ID 0x1020011的视图
码
main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
MainActivity.java
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
final TabHost tabs = getTabHost();
TabHost.TabSpec spec;
Intent i;
i = new Intent(this,MyActivity.class);
spec = tabs.newTabSpec("MyActivity").setIndicator("MyActivity",res.getDrawable(R.drawable.tab)).setContent(i);
tabs.addTab(spec);
}
}
MyActivity.class
public class MyActivity extends FragmentActivity {
private static String TAG = "MyActivity";
private static FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListeResultatFragment fragment = MyFragment.newInstance();
fragmentTransaction.add(android.R.id.tabcontent,fragment,"MyFragment");
fragmentTransaction.commit();
}
}
MyFragment.java
public class MyFragment extends Fragment {
public static MyFragment newInstance() {
MyFragment instance = new MyFragment();
return instance;
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment,container,false);
}
}
Android Fragment FragmentTabHost 问题
今天需要做一个功能,实现 tab 切换功能,但是又不能向 viewpager 一样可以滑动,只能通过顶部的 tab 标签滑动,就是类似 ActionBar 的 tab 一样的切换。
然后我就去找例子,在 ApiDemos 中有 FragmentTabs(extends Activity),FragmentTabsFragment (extends Fragment),两个例子,特别说一下 FragmentTabsFragment,这个类中的 TabManager 是重写了 FragmentTabHost,自己实现的状态保存等等,值得参考一下,当然,这个类也参考了 FragmentTabHost 的实现。
在 Support4Demos 中,有 FragmentTabs (extends FragmentActivity),FragmentTabsFragmentSupport (extends Fragment);
我主要参考的是 FragmentTabsFragmentSupport,因为我想用 Fragment 去实现我的需求。
遇到的几个问题:
1.FragmentTabHost 的顶部 tab 样式是系统的,不符合我的要求,那么如何定制这个样式呢?
你一定是这样使用的:
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator(“simple”), ArrayListFragment.class, null);
如果要改这个 tab 的样式,可以这样:
Button simple = new Button(getActivity());
simple.setText("xxx");
simple.setTextColor(this.getResources().getColor(R.color.green));
// simple.setBackgroundColor(R.color.indicate);
simple.setBackgroundResource(R.drawable.ic_star_p);
// set padding
// simple.setPadding(150, 150, 0, 0);
// set margin
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT, 1); // 1 是可选写的
lp.setMargins(150, 50, 0, 50);
simple.setLayoutParams(lp);
看到了吧,可以设置 padding, margin, background 等。。。
然后 -》setIndicator (View view), 看到了吧,有个方法是支持自定义 view 的,所以我们就可以自定义一个 view,比如,把上面定义的 Button simple 传进去就可以了。
还可以设置 TabWidget 的高度和背景:
mTabHost.getTabWidget().setBackgroundResource(R.drawable.ic_new_tab_p);
mTabHost.getTabWidget().setMinimumHeight(300);
mTabHost.getTabWidget().setDividerDrawable(null);
2. 多个 tab 切换的时候,每次都会从新执行:onCreateView,onDestroyView,导致比如 listview 这种无法保存浏览的位置?
我是这样解决的:
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_pager_list, container, false);
}
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null) {
parent.removeView(rootView);
}
View tv = rootView.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
return rootView;
测试代码下载地址:
http://download.csdn.net/detail/song_shi_chao/7168045
参考:
http://www.cnblogs.com/asion/archive/2013/09/25/3339313.html
http://ar.newsmth.net/thread-ffd9fb821607d1.html
http://www.eoeandroid.com/thread-322096-1-1.html
http://www.byywee.com/page/M0/S910/910755.html
http://blog.csdn.net/renpengben/article/details/12615487(我使用了这种方法)
http://www.2cto.com/kf/201309/242225.html
http://www.eoeandroid.com/thread-153696-1-1.html(ViewPager 与其中的子 View 滑动冲突该如何解决)
Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)
采取的方法是Fragment+FragmentTabHost组件来实现这种常见的app主页面的效果
首先给出main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:background="@color/white" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/color_home_tab_line" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/et_divider_disable"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout> </LinearLayout>
主代码:
public class MainActivity { @ViewInject(android.R.id.tabhost) private FragmentTabHost mTabHost; private LayoutInflater layoutInflater; private int mImageViewArray[] = {R.drawable.home_tab1,R.drawable.home_tab2,R.drawable.home_tab3,R.drawable.home_tab4}; private String mTextviewArray[] = {"首页","圈子","资讯","个人中心"}; private Class fragmentArray[] = {Fragment1.class,Fragment2.class,Fragment3.class,Fragment4.class}; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } @Override protected void init() { // list=new JSONArray(); layoutInflater=LayoutInflater.from(this); initTabHost();//初始化底部菜单 } /** * 初始化底部工具栏 */ private void initTabHost() { mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent); int count = fragmentArray.length; for (int i = 0; i < count; i++) { TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]) .setIndicator(getTabItemView(i)); mTabHost.addTab(tabSpec,fragmentArray[i],null); mTabHost.getTabWidget().getChildAt(i) .setBackgroundResource(R.color.white); } mTabHost.setCurrentTabByTag(mTextviewArray[0]); mTabHost.getTabWidget().setDividerDrawable(null); } /** * 项的样式 * @param index 第几个 * @return 每一个Tab样式 */ private View getTabItemView(int index) { View view = layoutInflater.inflate(R.layout.tab_home_item,null); ImageView imageView = (ImageView) view.findViewById(R.id.icon); imageView.setimageResource(mImageViewArray[index]); TextView textView = (TextView) view.findViewById(R.id.name); textView.setText(mTextviewArray[index]); return view; } }
通过以上文章,希望能帮助到大家,谢谢大家对本站的支持!
android fragmenttabhost 如何让tabs位于底部(个人笔记)
方法一:
修改布局文件
<android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" <!--这个id很特别--> android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.LinearLayoutCompat android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@android:id/tabcontent" <!--这个id很特别--> android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <!--这个权重很特别--> <TabWidget android:id="@android:id/tabs" <!--这个id很特别--> android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"/> <!--这个权重很特别--> </android.support.v7.widget.LinearLayoutCompat> </android.support.v4.app.FragmentTabHost>
代码如下:
mTabHost = findViewById(android.R.id.tabhost); mTabHost.setup(this,getSupportFragmentManager(),android.R.id.tabcontent); mTabHost.addTab(mTabHost.newTabSpec("First").setIndicator("First"), ThirdFragment.class,null); mTabHost.addTab(mTabHost.newTabSpec("Second").setIndicator("Second"), FourthFragment.class,null); mTabHost.addTab(mTabHost.newTabSpec("Third").setIndicator("Third"), ThirdFragment.class,null); mTabHost.addTab(mTabHost.newTabSpec("Fourth").setIndicator("Fourth"), FourthFragment.class,null);
方法二:
https://github.com/jessicass/fragment2/blob/master/res/layout/activity_main.xml
今天关于FragmentTabHost使用方法详解和host_framerate的讲解已经结束,谢谢您的阅读,如果想了解更多关于Android 1.6和Fragment&Tabhost、Android Fragment FragmentTabHost 问题、Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)、android fragmenttabhost 如何让tabs位于底部(个人笔记)的相关知识,请在本站搜索。
本文标签: