【发布时间】:2015-03-28 17:06:34
【问题描述】:
在我的应用程序中,我有一个 ActionBarActivity(我正在使用带有 AppCompat 的支持库),它使用 Google 的 SlidingTabLayout 类(取自 here)。所以这是 Activity 布局的 XML 代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_series_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SeriesDetailsActivity">
<com.my.package.SlidingTabLayout
android:id="@+id/series_details_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/primary_material_dark" />
<android.support.v4.view.ViewPager
android:id="@+id/series_details_pager"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
在这个活动中,当用户在操作栏中按下一个选项时,我想添加一个带有自定义动画的片段。这是处理菜单点击的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
/* ... OTHER CASES ... */
case R.id.menu_voption:
newFragment = MyNewFragment.newInstance();
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(
R.anim.slide_up,
R.anim.slide_down
)
.add(R.id.activity_series_details, newFragment)
.commit();
editing = true;
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
这样做,我的新片段被正确添加到活动中并替换当前片段而不是选项卡布局,它仍然可见。所以我尝试在开始交易之前添加这一行:
tabsHost.setVisibility(View.GONE);
其中tabsHost 是SlidingTabLayout。通过此修改,选项卡布局消失并且新片段正确显示,但仅在 API 级别 >= 21 中。在我的三星 Galaxy S4(运行 API 19)和所有其他 API 级别低于 21 的模拟器中(我的目标是 11+),选项卡布局消失但新片段不可见。我很确定是我的错,但我不知道为什么。谢谢大家的关注。
【问题讨论】:
标签: android android-fragments android-appcompat android-tabs