我在安卓应用程序中使用了选项卡界面,并将FrameLayout作为主要布局。但是我收到了一个安卓Lint警告,上面写着:
This <FrameLayout> can be replaced with a <merge\> tag
我只在onTabSelected监听器中使用这个FrameLayout(名为fragmentContainer)。以下是一个简化版本。
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, show the tab contents in the
// container
Fragment fragment;
String tag;
switch (tab.getPosition()) {
case 0:
fragment = new myFragment1();
tag = "myFragment1";
break;
case 1:
fragment = new new myFragment2();
tag = "myFragment2";
break;
default:
Log.d(TAG, "invalid tab selected: " + tab.getPosition());
break;
}
fragmentTransaction.replace(R.id.fragmentContainer, fragment, tag);
}
这段代码已经运行良好,但是如果可能的话,我希望能够进一步提高性能并消除任何Lint警告。如果我使用merge标签替换FrameLayout,则无法调用fragmentTransaction(),因为id在任何地方都不存在。
以下是FrameLayout的完整代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />