【发布时间】:2016-01-29 19:40:04
【问题描述】:
我正在尝试创建布局从屏幕底部向上浮动的应用程序,但我在屏幕底部有选项卡,所以我想用布局动画向上浮动来隐藏它们。我的问题是如何访问其他 xml 文件中的选项卡布局,而不是已经在动画中使用的 RelativeLayout。除了用于动画的文件之外,我还在其他文件中定义了 TabLayout。
我的 xml 文件看起来像那样
片段活动:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.young.howittastes.FragmentsActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="5dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
android:id="@+id/viewPager"
android:background="#d8d8d8"/>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tab_layout"
app:tabMode="fixed"
android:background="?attr/colorPrimary"
android:elevation="6dp"
app:tabTextColor="#d3d3d3"
app:tabSelectedTextColor="#ffffff"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
还有家庭片段:
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerlayout">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="animation"
android:id="@+id/animationButton"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#d3d3d3"
android:id="@+id/commentLayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:elevation="6dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WRITE"
android:textSize="14sp"
android:textColor="#000"
android:textStyle="bold"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="READ"
android:textSize="14sp"
android:textColor="#030303"
android:textStyle="normal"
android:layout_marginTop="4dp"
android:layout_marginRight="4dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</RelativeLayout>
Fragment 具有 onCreateView 和 ContentAdapter 类。 TabLayout 是这样定义的,但它是在我使用动画的 Fragment 之外的其他文件中定义的:
TabLayout tabs = (TabLayout) findViewById(R.id.tab_layout);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HomeContent(), "Home");
adapter.addFragment(new FavoritesContent(), "Favorite");
adapter.addFragment(new FavoritesContent(), "Search");
adapter.addFragment(new FavoritesContent(), "Write");
viewPager.setAdapter(adapter);
tabs.setupWithViewPager(viewPager);
【问题讨论】:
标签: android android-fragments android-tablayout