我有一个填充了三个片段的视图寻呼机,标签表示为点。我为选定或默认状态创建了一个具有不同drawable的选择器,它可以正常工作。但是,对于每个选定的片段,相应的点指示器必须具有不同的颜色。为此,我实现了以下内容,但在tabSelectedListener上设置的颜色更改没有效果,颜色仍然是dot_selector文件中指定的颜色。
PagerAdapter pagerAdapterWelcome = new PagerAdapter(getSupportFragmentManager(), listFragments);
viewPager.setAdapter(pagerAdapterWelcome);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
tabLayout.setupWithViewPager(viewPager, true);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(android.R.color.white));
break;
case 1:
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(android.R.color.black));
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(android.R.color.darker_gray));
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(android.R.color.white));
break;
case 1:
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(android.R.color.black));
break;
}
}
}
布局:
<RelativeLayout 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.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabDots"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@drawable/dot_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"/>
</RelativeLayout>
dot_selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/dot_indicator_selected"
android:state_selected="true"/>
<item android:drawable="@drawable/dot_indicator_default"/>
</selector>
dot_indicator_selected:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="4dp"
android:useLevel="false">
<solid android:color="@android:color/black"/>
</shape>
</item>
</layer-list>
dot_indicator_default:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="4dp"
android:useLevel="false">
<solid android:color="@android:color/darker_gray"/>
</shape>
</item>
</layer-list>
答案 0 :(得分:0)
这在我的应用程序中对我有用
在布局资源
中 <LinearLayout
android:id="@+id/layoutDots"
android:layout_width="match_parent"
android:layout_height="@dimen/dots_height"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/dots_margin_bottom"
android:gravity="center">
</LinearLayout>
在onCreate方法
dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
addBottomDots(numberOfDotsYouWant);
addBottomDots()方法
private void addBottomDots(int currentPage)
{
dots = new TextView[layouts.length];
dotsLayout.removeAllViews();
for(int i = 0; i < dots.length; i++)
{
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
dots[i].setTextColor(getColor(dot_light));
else
dots[i].setTextColor(Color.LTGRAY);
dotsLayout.addView(dots[i]);
}
if(dots.length > 0)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
dots[currentPage].setTextColor(getColor(dot_dark));
else
dots[currentPage].setTextColor(Color.GRAY);
}
}