如何在android中添加片段内的片段?

时间:2014-06-02 06:43:35

标签: java android

我尝试在片段内添加制表符。我可以添加,但特定制表符的内容与制表符重叠

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中

<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

 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        

 <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

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              

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

 </LinearLayout>

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            

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

   </LinearLayout>

屏幕截图:

enter image description here

任何建议的帮助都会有所帮助,我想要标签标题下面的标签片段的内容。      片段的内容与标题标题重叠。我使用FragmentTabHost在片段强文本中使用标签布局 请帮我解决这个问题。我想要标签页下面的标签片段的内容。

1 个答案:

答案 0 :(得分:3)

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

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;
}