如何在android中的选定选项卡上设置单个背景图像
我对android非常陌生,我尝试在选择的选项卡上显示个人总背景,但我对总背景做了这样的操作,当我更改另一个选项卡时,我需要每个选项卡的个人图像,请在我的代码下面帮助我,我需要每个选项卡的类似图像 XML布局:如何在android中的选定选项卡上设置单个背景图像,android,android-tablayout,Android,Android Tablayout,我对android非常陌生,我尝试在选择的选项卡上显示个人总背景,但我对总背景做了这样的操作,当我更改另一个选项卡时,我需要每个选项卡的个人图像,请在我的代码下面帮助我,我需要每个选项卡的类似图像 XML布局: <LinearLayout android:id="@+id/main_layout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas
<LinearLayout
android:id="@+id/main_layout"
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"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@drawable/imag_bg" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="AppName"
android:id="@+id/textView"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:textSize="55dp"
android:layout_gravity="center_horizontal" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:layout_marginTop="25dp"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</LinearLayout>
您可以使用ViewPager.onPageChangeListener
并相应地更改主布局背景
LinearLayout mMainLayout = findViewById(R.id.main_layout);//get the main_layout reference
ViewPager viewPager = findViewById(R.id.pager);//get the ViewPager reference
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
//set the background according each tab/page
if(position == 0){
mMainLayout.setBackgroundResource(R.drawable.imag_bg);
}else{
mMainLayout.setBackgroundResource(R.drawable.image_2);//change to your alternative image
}
}
});
您可以使用ViewPager.onPageChangeListener
并相应地更改主布局背景
LinearLayout mMainLayout = findViewById(R.id.main_layout);//get the main_layout reference
ViewPager viewPager = findViewById(R.id.pager);//get the ViewPager reference
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
//set the background according each tab/page
if(position == 0){
mMainLayout.setBackgroundResource(R.drawable.imag_bg);
}else{
mMainLayout.setBackgroundResource(R.drawable.image_2);//change to your alternative image
}
}
});