[英]There is no tab background attribute in the java code to change tab background dynamically
如何通过java代码动态更改选项卡背景这里是我的布局代码。如果我可以获得应用程序:tabBackground =“@ drawable / tab_background”在java代码中然后我可以通过位置更改背景drawable选项卡选择使用选择器来获得所需的屏幕,如第一张图像,但我无法找到任何地方,请帮助我的朋友
<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.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
创建一个监听器tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
和
tabLayout.setSelectedTabIndicatorHeight((int) (2 * getResources().getDisplayMetrics().density));
尝试这个:
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.