我知道有關於這個問題的SO
上發佈了各種問題,但他們都沒有爲我提供解決方案。我在fragment activity
中有4個選項卡,每個選項卡都有自己的fragment
。我將進入另一個fragment
裏面。當我切換到標籤有時會變得重疊,都是可見的。片段在另一個片段上重疊
下面是代碼(XML):
<Button
android:id="@+id/checkBal"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:clickable="false"
android:gravity="left|center_vertical"
android:typeface="serif" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:background="@color/green" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
public class Tab_Activity extends FragmentActivity {
FragmentTabHost mHost;
Button balance;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
balance = (Button) findViewById(R.id.checkBal);
mHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mHost.addTab(mHost.newTabSpec("Home").setIndicator("Home"),
Home_Fragment.class, null);
mHost.addTab(mHost.newTabSpec("Number").setIndicator("Send to number"),
Number_Fragment.class, null);
mHost.addTab(
mHost.newTabSpec("Contact").setIndicator("Send to contacts"),
Contact_Fragment.class, null);
mHost.addTab(mHost.newTabSpec("Group").setIndicator("Send to groups"),
Group_Fragment.class, null);
mHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
if (tabId.equalsIgnoreCase("Home")) {
Home_Fragment home = new Home_Fragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, home, "Home");
ft.commit();
}
if (tabId.equalsIgnoreCase("Number")) {
Number_Fragment number = new Number_Fragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, number, "Number");
ft.commit();
}
if (tabId.equalsIgnoreCase("Contact")) {
Contact_Fragment contact = new Contact_Fragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, contact, "Contact");
ft.commit();
}
if (tabId.equalsIgnoreCase("Group")) {
Group_Fragment group = new Group_Fragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, group, "Group");
ft.commit();
}
}
});
}
public void newFragment(Fragment fragment, String tag) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
//
if (tag.equalsIgnoreCase("Group"))
ft.remove(new Group_Fragment());
else
ft.remove(new Contact_Fragment());
ft.replace(R.id.realtabcontent, fragment);
ft.addToBackStack(null);
ft.commit();
}
public void updateContacts(ArrayList<String> cList) {
FragmentManager manager = getSupportFragmentManager();
Contact_Fragment contactFrag = (Contact_Fragment) manager
.findFragmentByTag("Contact");
contactFrag.updateData(cList);
}
}
內新片段
mActivity.newFragment(new Select_Group_Fragment(), "Group");
由於我C調用片段從另一個片段阿靈片段,我需要在new fragment
方法Tab_Activity
請幫傢伙
的圖像可能是有用的,以顯示該重疊 – dymmeh
overlapp可由佈局引起的。如果使用'RelativeLayout'而不指定視圖將出現的順序或'FrameLayout',這種不可預知的行爲可能發生 –
@dotroph截圖附帶 – moDev