如何在Android中的片段内添加标签? [英] How to add tab inside fragment in android?

查看:67
本文介绍了如何在Android中的片段内添加标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在片段内添加标签.我可以添加,但特定标签的内容与标签重叠

I try to add tab inside fragment .I can add but the content of particular tab overlap the tab

MainFragmentActivity.java

MainFragmentActivity.java

   public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    Fragment fragment= new FragmentTab();
        getSupportFragmentManager().beginTransaction().add(R.id.content_frame,fragment).commit();
 }

   }

activity_main.xml

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />  
   </LinearLayout>

FragmentTab.java

FragmentTab.java

 public class FragmentTab extends Fragment {
public FragmentTab() {

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

}

FragmentTabHost mTabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  
       savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.fragment_tabs, container, false);

    mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment A"),
            FragmentA.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment B" ),
            FragmentB.class, null);

    return rootView;
}

 }

fragment_tabs.xml

fragment_tabs.xml

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

  </android.support.v4.app.FragmentTabHost>

FragmentA.java

FragmentA.java

public class FragmentB extends Fragment{

@Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
           savedInstanceState) {
    View rootView= inflater.inflate(R.layout.fragment_a, container,false);
        return rootView;
}
  }

fragment_a.xml

fragment_a.xml

 <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="FragmentA" />

 </LinearLayout>

FragmentB.java

FragmentB.java

 public class FragmentB extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  
            savedInstanceState) {
        View rootView= inflater.inflate(R.layout.fragment_b, container,false);
        return rootView;
}
  }

fragment_b.xml

fragment_b.xml

  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="FragmentB" />

   </LinearLayout>

屏幕截图:

任何建议的帮助都会有所帮助,我希望选项卡标题下面的选项卡片段的内容.片段的内容与选项卡标题重叠.我使用FragmentTabHost在片段中使用选项卡布局强文本请帮助我解决此问题.我希望选项卡片段的内容位于选项卡标题下方.

Any help of suggestion would be helpful, I want the content of tab fragment below tab header. The content of fragment overlap the tab header.I use FragmentTabHost to use tab layout in fragmentstrong text Please help me how to solve this issue.I want the content of tab fragment below tab header.

推荐答案

如果您的目标是API17 +,我相信这可能对您有用.如果没有,您应该看看ViewContainers和可滑动查看的视图.

I believe this could work for you, if you are targeting API17 +. If not you should take a look at ViewContainers, and swipeable views.

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

public class TabHostParentFragment extends Fragment {

private FragmentTabHost tabHost;

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

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

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

return tabHost;
}

这篇关于如何在Android中的片段内添加标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆