Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android appbar布局中的图像_Android_Android Tablayout_Android Appbarlayout - Fatal编程技术网

Android appbar布局中的图像

Android appbar布局中的图像,android,android-tablayout,android-appbarlayout,Android,Android Tablayout,Android Appbarlayout,我正在做一个选项卡式活动。我在AppBarLayout中放置了一个图像视图。我想在选择每个选项卡时更改图像视图src或背景 有人给我一些提示吗 我在下面附上了我的代码 <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_conten

我正在做一个选项卡式活动。我在
AppBarLayout
中放置了一个图像视图。我想在选择每个选项卡时更改图像视图src或背景

有人给我一些提示吗

我在下面附上了我的代码

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

        <ImageView
            android:id="@+id/appBarImg"
            android:layout_width="400dp"
            android:layout_height="200dp"
            android:layout_weight="1"
            android:background="@drawable/sc" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="#FFF"
            app:tabMode="scrollable"
            app:tabPaddingEnd="15dp"
            app:tabPaddingStart="15dp">

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Schengen" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Rest Of World" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Worldwide" />

        </android.support.design.widget.TabLayout>
    </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" />

试试这个

    ImageView appBarImg=findViewById(R.id.appBarImg);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            if(position==0){
                appBarImg.setBackgroundResource(R.drawable.position1);
            }else if(position==1){
                appBarImg.setBackgroundResource(R.drawable.position2);
            }else if(position==2){
                appBarImg.setBackgroundResource(R.drawable.position3);
            }
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
编辑

<android.support.design.widget.AppBarLayout
        android:id="@+id/htab_appbar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:visibility="visible">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <ImageView
                android:id="@+id/appBarImg"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/ic_launcher_background"
                app:layout_collapseMode="parallax" />

        </LinearLayout>





       <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="#FFF"
            app:tabMode="scrollable"
            app:tabPaddingEnd="15dp"
            app:tabPaddingStart="15dp">

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Schengen" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Rest Of World" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Worldwide" />

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

试试这个

    ImageView appBarImg=findViewById(R.id.appBarImg);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            if(position==0){
                appBarImg.setBackgroundResource(R.drawable.position1);
            }else if(position==1){
                appBarImg.setBackgroundResource(R.drawable.position2);
            }else if(position==2){
                appBarImg.setBackgroundResource(R.drawable.position3);
            }
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
编辑

<android.support.design.widget.AppBarLayout
        android:id="@+id/htab_appbar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:visibility="visible">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <ImageView
                android:id="@+id/appBarImg"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/ic_launcher_background"
                app:layout_collapseMode="parallax" />

        </LinearLayout>





       <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="#FFF"
            app:tabMode="scrollable"
            app:tabPaddingEnd="15dp"
            app:tabPaddingStart="15dp">

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Schengen" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Rest Of World" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Worldwide" />

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


ImageView-appBarImg=findviewbyd(R.id.appBarImg);问题是上面的线。我无法将appbarimg分配给图像对象。未获得u@MohammadaMirandroid studio不允许我将图像视图分配给appbarimg对象。我已尝试上述onPageSelected方法。但图像视图分配失败阻止了它。你知道吗@Nilesh RathodImageView appBarImg=findViewById(R.id.appBarImg);问题是上面的线。我无法将appbarimg分配给图像对象。未获得u@MohammadaMirandroid studio不允许我将图像视图分配给appbarimg对象。我已尝试上述onPageSelected方法。但图像视图分配失败阻止了它。你知道吗@尼利斯·拉托德