【问题标题】:how to add tabhost in fragments如何在片段中添加tabhost
【发布时间】:2015-09-25 04:02:47
【问题描述】:

我正在尝试在片段中添加 tabhost,但无论我尝试什么,我都无法插入它。我可能在这里遗漏了一些基础知识。这里是我的类 TabFragment 的代码。它返回一个视图。

public class TabFragment extends Fragment{  

    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
    }
    private TabHost mTabHost;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {
          View view = inflater.inflate(R.layout.tabmain, container, false);
          mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
          mTabHost.setup();//very important to call this
          TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
          tab.setIndicator("my tab content");
          mTabHost.addTab(tab);
      return view;
    }
}

【问题讨论】:

标签: android android-layout


【解决方案1】:

使用 API 级别 17,现在可以做到这一点:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

// This class is the 3rd fragment in my ViewPager, 
// to which I wanted to add 2 tabs....
public class TabHostParentFragment extends Fragment {
private FragmentTabHost mTabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);

Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"),
    MyNestedFragment1.class, arg1);

Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"),
    MyNestedFragment2.class, arg2);

return mTabHost;
}

@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}

确保更新您的 android-support-v4.jar 文件,因为当我通过 SDK 管理器下载时,它没有自动更新。这可以防止定义 getChildFragmentManger() 函数。

【讨论】:

  • 这也适用于我使用最新的 android-support-v4 库。
  • 有没有办法在不使用 support.vx 库的情况下做到这一点?
  • @jamison 在这里查看..canyou help stackoverflow.com/questions/28106944/…
  • 非常感谢 :) 你能告诉我如何自定义标签吗?我想更改下划线颜色,使文本小写,并更改颜色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多