我对 Android 编程很陌生,但我会尽力提供所有需要的信息。我正在使用新的android.support.v7.app.ActionBarActivity
并android.support.v4.app.Fragment
显示从 8 到 17 的 Android API 的选项卡布局。我在我的活动中正确显示我的两个片段时遇到问题,因为在我选择其中一个片段后它们会相互重叠。所以,这是我的主要活动代码:
package it.koopa.scank;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends ActionBarActivity {
private final static String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//have to use getSupportActionBar from android.support.v7.app
ActionBar actionBar = getSupportActionBar();
//hello tab
Tab tab = actionBar.newTab()
.setText(R.string.tab_hello)
.setTabListener(new TabListener<HelloFragment>(this, "hello", HelloFragment.class));
actionBar.addTab(tab);
//handle content tab
tab = actionBar.newTab()
.setText(R.string.tab_send)
.setTabListener(new TabListener<HandleContentFragment>(this, "handle", HandleContentFragment.class));
actionBar.addTab(tab);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.scank, menu);
return true;
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final ActionBarActivity mActivity;
private final String mTag;
private final Class<T> mClass;
/**
* Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(ActionBarActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(getCorrectActionBarId(), mFragment, mTag);
Log.i(TAG, "FragID " + mFragment.getId() + ", FragTAG=" + mFragment.getTag() + " ADDED!!!");
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
Log.i(TAG, "FragID " + mFragment.getId() + ", FragTAG=" + mFragment.getTag() + " attached.");
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
Log.i(TAG, "FragID " + mFragment.getId() + ", FragTAG=" + mFragment.getTag() + " detached.");
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
/**
* Returns the correct id of the action bar
* @return
*/
public static int getCorrectActionBarId () {
int androidVersion = Build.VERSION.SDK_INT;
if (androidVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return android.R.id.content;
} else {
return R.id.action_bar_activity_content;
}
}
}
这是我的两个片段(都使用自己的 xml 布局)。第一:
package it.koopa.scank;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HelloFragment extends Fragment {
private int index;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello, container, false);
Log.i("HelloFragment","I'm " + HelloFragment.class);
return v;
}
}
第二个:
package it.koopa.scank;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HandleContentFragment extends Fragment {
private int index;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.handle_content, container, false);
Log.i("HandleContentFragment","I'm " + HandleContentFragment.class);
return v;
}
}
结果,如果我选择第二个选项卡,视图就会一个接一个!我在Tabs using android.support.v7.app.ActionBar找到了一个类似的问题,但接受的解决方案似乎对我不起作用。事实上,在我的主要活动中,您可以看到我获得了内容 ID
int androidVersion = Build.VERSION.SDK_INT;
if (androidVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return android.R.id.content;
} else {
return R.id.action_bar_activity_content;
}
但是片段内容仍然被覆盖(由于我的声誉几乎为零,我无法上传图片)。我哪里错了?
更新:我的活动使用android:theme="@style/Theme.AppCompat.Light"
. 我正在使用 Android 4.2.1 在 Nexus i9250 上进行测试