如何在显示一个片段时显示工具栏中的按钮

时间:2017-09-07 21:02:11

标签: android android-fragments

我尝试只显示一个ImageButton,当我滑动到我的一个片段,所以如果我刷到一个不同的片段,Visibillity应该去Invisible但我试图做以下不幸的是它不起作用我的按钮的visibillity是可见的在所有片段中。 我没找到任何教程或类似的东西

这是我试过的代码:

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        switch (position) {
            case 0:
                firstfragment loc = new firstfragment();
                settings.setVisibility(View.VISIBLE);
                return loc;

            case 1:
                secondfragment qra = new secondfragment();
                settings.setVisibility(View.INVISIBLE);
                return qra;
            case 2:
               thirdfragment pre = new thirdfragment();
                settings.setVisibility(View.INVISIBLE);
            return pre;


            default:
                return null;


        }
    }

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.hartl.pxp.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"

        app:popupTheme="@style/AppTheme.PopupOverlay">
  <RelativeLayout
  android:layout_width="match_parent"
   android:layout_height="match_parent"
  android:orientation="horizontal"
  android:weightSum="1">


<ImageButton
    android:id="@+id/settingsbtn"
    android:layout_width="20dp"
    android:layout_height="match_parent"

    android:background="@color/test"
    android:foregroundGravity="center_vertical|center_horizontal"
    android:scaleType="fitCenter"
    android:src="@drawable/settings"
    android:layout_marginRight="28dp"
    android:layout_marginEnd="28dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"

    android:foregroundGravity="center_vertical|center_horizontal"
    android:gravity="center_vertical|center_horizontal"
    android:text="My App"
    android:textColor="@color/white"
    android:textSize="25sp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
  </RelativeLayout>
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

0 个答案:

没有答案