圆形指示器不可见

时间:2018-04-06 10:40:21

标签: android android-viewpager

我不知道为什么Circle Indicator在横幅布局中不可见。我正在使用图书馆"me.relex:circleindicator"。但它没有用。我想在我的imageView下面使用circleIndicator,但它不可见。

下面显示的屏幕截图显示了我想要圆圈指示符的位置。

这是我在mainactivity中的代码:

 public class MainActivity extends AppCompatActivity {

    DialogInterface.OnClickListener listener;
    @BindView(R.id.vp_mainLayout)
    ViewPager viewPager;
    @BindView(R.id.circleindicator)
    CircleIndicator circleIndicator;
    //    private ViewPager viewPager;
    //    private CircleIndicator circleIndicator;
    int gambar[] = {R.drawable.banner1, R.drawable.banner2, 
    R.drawable.no_photo, 
    R.drawable.peta_indonesia};
    private int currentPage = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        getSupportActionBar().setBackgroundDrawable(new 
        ColorDrawable(Color.parseColor("#300000ff")));
        getSupportActionBar().setBackgroundDrawable(new 
        ColorDrawable(Color.TRANSPARENT));
        getSupportActionBar().setStackedBackgroundDrawable(new 
        ColorDrawable(Color.parseColor("#550000ff")));

        BannerAdapter bannerAdapter = new BannerAdapter(gambar, 
        MainActivity.this);
        viewPager.setAdapter(bannerAdapter);
        circleIndicator.setViewPager(viewPager);


bannerAdapter.registerDataSetObserver(circleIndicator.getDataSetObserver());

这个我的适配器:

public class BannerAdapter extends PagerAdapter {

int[] gambar;
Activity activity;

public BannerAdapter(int[] gambar, Activity activity) {
    this.gambar = gambar;
    this.activity = activity;
}

@Override
public int getCount() {
    return gambar.length;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater = (LayoutInflater) 
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.item_banner, container, false);
    ImageView img = v.findViewById(R.id.imgbanner);

    Glide.with(activity)
            .load(gambar[position])
            .into(img);

    container.addView(v);
    return v;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}
}

这是我的XML:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="230dp"
    app:cardCornerRadius="0dp">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 
</android.support.v4.view.ViewPager>

    <me.relex.circleindicator.CircleIndicator
        android:id="@+id/circleindicator"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_gravity="bottom"
        app:ci_gravity="center" />
</android.support.v7.widget.CardView>
</LinearLayout>

这是我的项目横幅:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imgbanner"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:scaleType="centerCrop" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

默认情况下,CircleIndicator的颜色为白色,并且因为布局的默认颜色为白色。 CircleIndicator不可见。

您可以在XML文件中使用以下属性使其可见

app:ci_drawable="@drawable/white_radius_circle_indicator"
app:ci_drawable_unselected="@drawable/black_radius_circle_indicator"

此处 app:ci_drawable 用于选定的圈子,而 app:ci_drawable_unselected 用于未选定的圈子。

res / drawable / white_radius_circle_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
    android:color="@color/accent"/>
</shape>

res / drawable / black_radius_circle_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
    android:color="@android:color/black"/>
</shape>

答案 1 :(得分:0)

只需通过以下方式提升您的圆形页面指示器:

android:translationZ="4dp"

至少对我有用