Android 选择器改变颜色但不改变大小
Android selector changes color but not size
我正在尝试使用选择器为 ViewPager 实现一个简单的点指示器。
当页面改变时,点应该改变颜色和大小,但它们只改变颜色,而大小保持不变。
选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="oval">
<size
android:height="12dp"
android:width="12dp"/>
<solid android:color="@color/highlight"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<size
android:height="6dp"
android:width="6dp"/>
<solid android:color="@color/light_grey"/>
</shape>
</item>
</selector>
在布局中使用
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/margin_2x"
android:src="@drawable/dot_selector">
</ImageView>
在onCreate()中用于显示初始状态:
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
getLayoutInflater().inflate(R.layout.dot_indicator, viewPagerCountDots);
}
viewPagerCountDots.getChildAt(0).setSelected(true);
这看起来符合预期:
但是当我像这样更改页面时更改选择器状态时...
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
viewPagerCountDots.getChildAt(i).setSelected(i == position);
}
}
..看起来像这样:
使子项或 ViewGroup 无效不会执行任何操作。知道这里出了什么问题吗?
更新
根据
的建议,对子项调用 requestLayout 修复了它
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
View dotView = viewPagerCountDots.getChildAt(i);
dotView.setSelected(i == position);
dotView.invalidate();
dotView.requestLayout();
}
}
视图大小在创建时测量一次。由于您将 ImageView 的布局宽度和高度设置为 "wrap_content",未选中的圆圈将具有 6dp 大小并且它们的大小不会改变。可以设置布局宽高"12dp".
顺便说一句,我建议使用指标库而不是实施它。 ViewPagerIndicator 不错
这样试试
//if you container is LinearLayout ,change to LinearLayout.params
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//as you wish
params.width=200;
params.height=200;
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
ImageView dotIv= (ImageView)viewPagerCountDots.getChildAt(i);
dotIv.setImageResource("//normalPic");
if(i==position){
dotIv.setImageResource("//selectPic");
dotIv.setLayoutParams(params);
}
}
}
在您的 onPageSelected
回调中添加 requestLayout()
调用:
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
viewPagerCountDots.getChildAt(i).setSelected(i == position);
viewPagerCountDots.getChildAt(i).requestLayout();
}
}
编辑:
或者您可以将可绘制对象更改为始终具有相同的大小,因此 setSelected
就足够了:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="oval">
<size
android:width="12dp"
android:height="12dp"/>
<solid android:color="@color/highlight"/>
</shape>
</item>
<item>
<layer-list>
<item android:bottom="3dp"
android:left="3dp"
android:right="3dp"
android:top="3dp">
<shape android:shape="oval">
<size
android:width="6dp"
android:height="6dp"/>
<solid android:color="@color/light_grey"/>
</shape>
</item>
</layer-list>
</item>
</selector>
我正在尝试使用选择器为 ViewPager 实现一个简单的点指示器。
当页面改变时,点应该改变颜色和大小,但它们只改变颜色,而大小保持不变。
选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="oval">
<size
android:height="12dp"
android:width="12dp"/>
<solid android:color="@color/highlight"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<size
android:height="6dp"
android:width="6dp"/>
<solid android:color="@color/light_grey"/>
</shape>
</item>
</selector>
在布局中使用
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/margin_2x"
android:src="@drawable/dot_selector">
</ImageView>
在onCreate()中用于显示初始状态:
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
getLayoutInflater().inflate(R.layout.dot_indicator, viewPagerCountDots);
}
viewPagerCountDots.getChildAt(0).setSelected(true);
这看起来符合预期:
但是当我像这样更改页面时更改选择器状态时...
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
viewPagerCountDots.getChildAt(i).setSelected(i == position);
}
}
..看起来像这样:
使子项或 ViewGroup 无效不会执行任何操作。知道这里出了什么问题吗?
更新
根据
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
View dotView = viewPagerCountDots.getChildAt(i);
dotView.setSelected(i == position);
dotView.invalidate();
dotView.requestLayout();
}
}
视图大小在创建时测量一次。由于您将 ImageView 的布局宽度和高度设置为 "wrap_content",未选中的圆圈将具有 6dp 大小并且它们的大小不会改变。可以设置布局宽高"12dp".
顺便说一句,我建议使用指标库而不是实施它。 ViewPagerIndicator 不错
这样试试
//if you container is LinearLayout ,change to LinearLayout.params
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//as you wish
params.width=200;
params.height=200;
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
ImageView dotIv= (ImageView)viewPagerCountDots.getChildAt(i);
dotIv.setImageResource("//normalPic");
if(i==position){
dotIv.setImageResource("//selectPic");
dotIv.setLayoutParams(params);
}
}
}
在您的 onPageSelected
回调中添加 requestLayout()
调用:
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
viewPagerCountDots.getChildAt(i).setSelected(i == position);
viewPagerCountDots.getChildAt(i).requestLayout();
}
}
编辑:
或者您可以将可绘制对象更改为始终具有相同的大小,因此 setSelected
就足够了:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="oval">
<size
android:width="12dp"
android:height="12dp"/>
<solid android:color="@color/highlight"/>
</shape>
</item>
<item>
<layer-list>
<item android:bottom="3dp"
android:left="3dp"
android:right="3dp"
android:top="3dp">
<shape android:shape="oval">
<size
android:width="6dp"
android:height="6dp"/>
<solid android:color="@color/light_grey"/>
</shape>
</item>
</layer-list>
</item>
</selector>