【发布时间】:2016-02-11 11:11:55
【问题描述】:
这种类型的问题之前已经被问过,但没有得到任何合适的、有效的解决方案,所以我再次发布这个问题。抱歉再次询问并浪费您的时间。 请提供一些可行的解决方案。或者让我知道我是否做错了什么。 提前致谢。
但即将到来:
页面加载选项卡类似于“预期选项卡”图像,但选择后类似于“即将出现的选项卡”图像。 MainXML 代码:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_img_jpeg"
android:minHeight="10dp"
android:padding="10dp"
app:tabGravity="fill"
app:tabIndicatorColor="@color/TRANSPARENT"
app:tabMode="fixed"
app:tabTextColor="@color/blue" />
@style/MyCustomTabLayout
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabBackground">@drawable/tab_bg</item>
</style>
@drawable/tab_bg
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/tab_bgselected" />
<item android:drawable="@drawable/tab_bgnotselected" />
</selector>
@drawable/tab_bgselected
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="0dp" android:top="0dp"
android:left="0dp" android:right="0dp" >
<shape android:shape="rectangle">
<solid android:color="@color/blue" />
<corners
android:topLeftRadius="10dp"
android:bottomLeftRadius="10dp">
</corners>
</shape>
</item>
</layer-list>
像那样@drawable/tab_bgnotselected
在我写的后面的代码中:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
try {
mViewPager.setCurrentItem(tab.getPosition());
TabPosition = tab.getPosition();
TabCount = tabLayout.getTabCount();
try {
if (TabPosition == 0) {
GradientDrawable drawable = (GradientDrawable) getResources().getDrawable(R.drawable.policy_tab_blue);
drawable.setCornerRadii(new float[]{10,10,0,0,0,0,10,10}); // this will create the corner radious to left side
} else {
GradientDrawable drawable = (GradientDrawable) getResources().getDrawable(R.drawable.policy_tab_white);
drawable.setCornerRadii(new float[]{0,0,10,10,10,10,0,0}); // this will create the corner radious to right side
}
} catch (Exception e) {
e.printStackTrace();
}
Log.i("TabPosition:--->", TabPosition + "");
Log.i("TabCount:--->", TabCount + "");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
【问题讨论】:
标签: android android-layout android-tablayout