【问题标题】:How to use TabLayout in fragment如何在片段中使用 TabLayout
【发布时间】: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


    【解决方案1】:

    听起来您想将其中一个布局 XML 布局文件包含在另一个布局文件中。如果正确,您可以在封闭布局中使用&lt;include&gt; 标记。例如,如果 tabbar.xml 是您要包含的布局:

    <include layout="@layout/tabbar"/>
    

    更多详情请见this link

    【讨论】:

    • 如果我将只为标签栏制作其他 xml 文件并将其包含在两个 xml 文件中它不起作用,如果我只将它包含在处理片段的 xml 文件中并制作第二个视图以在其中定义它java 文件它不工作,没有第二个视图它也不工作。
    • 您能否更具体地说明什么不起作用? “第二个视图”是什么意思?当您说“java 文件”时,您指的是 XML 布局文件吗?
    猜你喜欢
    • 2023-03-24
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多