我想知道是否有可能在FragmentTabHost
之上插入视图,例如textview。在将保留Fragment
的{{1}}类中,我将在FragmentTabHost
方法中返回什么。当View OnCreateView(params)
顶部没有视图时很容易,因为我只能返回FragmentTabHost
的实例,但是当FragmentTabHost
顶部有视图时,它是怎么回事? 1}}?
我的主要活动:
FragmentTabHost
my activity_main.xml
public class MainActivity extends SherlockFragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
text.setText("hello world!");
mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
mTabHost.setCurrentTab(0);
ImageView house_iv = new ImageView(this);
house_iv.setImageDrawable(getResources().getDrawable(
R.drawable.icon_house_tab));
house_iv.setScaleType(ScaleType.FIT_XY);
ImageView card_iv = new ImageView(this);
card_iv.setImageDrawable(getResources().getDrawable(
R.drawable.icon_card_tab));
card_iv.setScaleType(ScaleType.FIT_XY);
ImageView upload_iv = new ImageView(this);
upload_iv.setImageDrawable(getResources().getDrawable(
R.drawable.icon_upload_tab));
upload_iv.setScaleType(ScaleType.FIT_XY);
ImageView settings_iv = new ImageView(this);
settings_iv.setImageDrawable(getResources().getDrawable(
R.drawable.icon_settings_tab));
settings_iv.setScaleType(ScaleType.FIT_XY);
Bundle b2 = new Bundle();
mTabHost.addTab(
mTabHost.newTabSpec("tab0").setIndicator(""),
AnotherFragment.class, b2);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator(house_iv),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator(card_iv),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator(upload_iv),
AnotherFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab4").setIndicator(settings_iv),
SendMessageFragment.class, null);
mTabHost.setCurrentTab(0);
mTabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
}
}
现在,AnotherFragment类未正确呈现。 my AnotherFragment.java:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
</ScrollView>
my fragment_another.xml
public class AnotherFragment extends SherlockFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.fragment_another, container, false);
TextView tv = (TextView) vg.findViewById(R.id.text);
tv.setText("hello world!");
FragmentManager fm = getChildFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragTwo = new SendMessageFragment();
ft.add(R.id.llFragmentContainer, fragTwo);
ft.commit();
fm.executePendingTransactions();
return vg;
}
}
仅渲染textview,但不渲染SendMessageFragment。 我的 SendMessageFragment.java:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello migs" />
<LinearLayout
android:id="@+id/llFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
最后,我的 fragment_sendmessage.xml:
public class SendMessageFragment extends SherlockFragment {
FragmentTabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static SendMessageFragment newInstance(String text) {
SendMessageFragment f = new SendMessageFragment();
Bundle b = new Bundle();
b.putString("text", text);
f.setArguments(b);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getSherlockActivity());
mTabHost.setup(getSherlockActivity(), getChildFragmentManager(),
R.layout.fragment_sendmessage);
ImageView tab_selfies = new ImageView(getSherlockActivity());
tab_selfies.setImageResource(R.drawable.sendmsg_tab_selfies);
ImageView tab_sendmsg = new ImageView(getSherlockActivity());
tab_sendmsg.setImageResource(R.drawable.sendmsg_tab_sendmsg);
ImageView tab_follow = new ImageView(getSherlockActivity());
tab_follow.setImageResource(R.drawable.sendmsg_tab_follow);
ImageView tab_misc = new ImageView(getSherlockActivity());
tab_misc.setImageResource(R.drawable.sendmsg_tab_misc);
mTabHost.addTab(mTabHost.newTabSpec("pane1").setIndicator(tab_selfies),
ProfileSelfies.class, null);
mTabHost.addTab(mTabHost.newTabSpec("pane2").setIndicator(tab_sendmsg),
ProfileSelfies.class, null);
mTabHost.addTab(mTabHost.newTabSpec("pane3").setIndicator(tab_follow),
ProfileSelfies.class, null);
mTabHost.addTab(mTabHost.newTabSpec("pane4").setIndicator(tab_misc),
ProfileSelfies.class, null);
return mTabHost;
}
}
全部。抱歉很长的问题。请帮助我谢谢。