我在Android中有一个选项卡式界面,并使用FrameLayout作为我应用程序中的主要布局。我收到一个安卓棉绒警告,上面写着:
This <FrameLayout> can be replaced with a <merge> tag
我使用此FrameLayout(名为fragmentContainer(的唯一位置是在onTabSelected侦听器中。 这是一个简化版本。
@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 警告。 如果我用合并标签替换 FrameLayout,我就不能调用那个 fragmentTransaction((,因为 id 在任何地方都不存在。
以下是完整性的框架布局:
<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" />
如果这是活动布局文件中的唯一内容,请删除布局文件和setContentView()
调用,并使用android.R.id.content
作为FragmentTransaction
的目标。 android.R.id.content
指向setContentView()
膨胀的FrameLayout
。
另外,请注意,操作栏选项卡在"L"开发者预览版中已弃用,在 Android 的下一个生产版本中应保持弃用状态。您可能希望考虑使用其他选项卡解决方案(例如,ViewPager
和选项卡式指示器(或其他导航解决方案(例如,导航抽屉(。