java代码中没有用于动态更改选项卡背景的选项卡背景属性
如何通过java代码动态更改选项卡背景这里是我的布局代码。如果我可以获得app:tabBackground=“@drawable/tab_background”在java代码中,我可以使用选择器通过选项卡选择的位置更改背景,以获得所需的屏幕,如第一幅图像,但我无法在任何地方找到它,请帮助我java代码中没有用于动态更改选项卡背景的选项卡背景属性,java,android,android-layout,android-tabs,android-tablayout,Java,Android,Android Layout,Android Tabs,Android Tablayout,如何通过java代码动态更改选项卡背景这里是我的布局代码。如果我可以获得app:tabBackground=“@drawable/tab_background”在java代码中,我可以使用选择器通过选项卡选择的位置更改背景,以获得所需的屏幕,如第一幅图像,但我无法在任何地方找到它,请帮助我 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schema
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="@+id/icon_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/toolbar_bg"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:tabBackground="@drawable/tab_background"
app:tabIndicatorColor="@color/transparent"
app:tabGravity="fill"
app:tabMode="fixed" />
<FrameLayout
android:id="@+id/main_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
但是我得到了这个布局
您需要为OnTabSelectedListener
事件以及每个项init.tabLayout.setSelectedTabidIndicatorColor(Color.parseColor(#FFFFFF”))创建一个侦听器代码>
及
试试这个:
tabLayout.getTabAt(1).setCustomView(R.drawable.your_layout); //2nd position
也可以使用自定义布局视图
tabLayout.getTabAt(1).setCustomView(R.layout.your_layout);
对于整体Tablayout
使用
tabLayout.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));
通过创建自定义布局,我如何管理未选择的选项卡背景如果你想管理“选择-取消选择”状态,你需要一个可绘制的选择器…在问题中,你问的是关于特定位置背景的编程<代码>是的,我使用“选择”来管理“选择”和“取消选择”状态,我需要更改应用程序:选项卡背景编程的,先生
tabLayout.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));