如何在android studio中为简介滑块绘制带边框的点?
我想在简介滑块中为每个幻灯片绘制点,用于简单点。我使用以下代码及其工作:如何在android studio中为简介滑块绘制带边框的点?,android,Android,我想在简介滑块中为每个幻灯片绘制点,用于简单点。我使用以下代码及其工作: TextView tv=new TextView(this); tv.setText(Html.fromHtml("•")); tv.setTextColor(ContextCompat.getColor(this,R.color.dot_active)); 但我想为非活动点绘制一个带白色边框的透明点。如下图所示: 如何才能做到这一点?在layout.xml文件中,为点添加此视图 <View
TextView tv=new TextView(this);
tv.setText(Html.fromHtml("•"));
tv.setTextColor(ContextCompat.getColor(this,R.color.dot_active));
但我想为非活动点绘制一个带白色边框的透明点。如下图所示:
如何才能做到这一点?在layout.xml文件中,为点添加此视图
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha=".5"
android:layout_above="@+id/layoutDots"
android:background="@android:color/white" />
在活动或片段中,使用此代码添加点
private LinearLayout dotsLayout;
private TextView[] dots;
private int no_of_dots = 4;
private void addBottomDots(int currentPage) {
dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
dots = new TextView[layouts.length];
dotsLayout.removeAllViews();
for (int i = 0; i < no_of_dots; i++) {
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
dots[i].setTextColor(getResources().getColor(Color.WHITE));
dots[i].setAlpha(0);
dots[i].setBackground(getResources().getDrawable(R.drawable.circle_background));
dotsLayout.addView(dots[i]);
}
if (dots.length > 0)
dots[currentPage].setTextColor(getResources().getColor(R.color.colorPrimary));
}
private LinearLayout dotsLayout;
私有文本视图[]点;
私有整数的点数=4;
私有void addBottomDots(int currentPage){
dotsLayout=(LinearLayout)findViewById(R.id.layoutDots);
dots=新文本视图[layouts.length];
dotsLayout.removeallview();
for(int i=0;i0)
dots[currentPage].setTextColor(getResources().getColor(R.color.colorPrimary));
}
将其添加到可绘制文件夹中。并将其设置为非活动点的背景
circle\u background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#78d9ff"/>
</shape>
编辑:
在上面的教程中,作者解释了如何使用viewpager实现Intro ImageSlider。你也可以看到他是如何实现dots布局的。
我已经实现了使用这个作为参考。希望有帮助。在layout.xml文件中,添加此点视图
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha=".5"
android:layout_above="@+id/layoutDots"
android:background="@android:color/white" />
在活动或片段中,使用此代码添加点
private LinearLayout dotsLayout;
private TextView[] dots;
private int no_of_dots = 4;
private void addBottomDots(int currentPage) {
dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
dots = new TextView[layouts.length];
dotsLayout.removeAllViews();
for (int i = 0; i < no_of_dots; i++) {
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
dots[i].setTextColor(getResources().getColor(Color.WHITE));
dots[i].setAlpha(0);
dots[i].setBackground(getResources().getDrawable(R.drawable.circle_background));
dotsLayout.addView(dots[i]);
}
if (dots.length > 0)
dots[currentPage].setTextColor(getResources().getColor(R.color.colorPrimary));
}
private LinearLayout dotsLayout;
私有文本视图[]点;
私有整数的点数=4;
私有void addBottomDots(int currentPage){
dotsLayout=(LinearLayout)findViewById(R.id.layoutDots);
dots=新文本视图[layouts.length];
dotsLayout.removeallview();
for(int i=0;i0)
dots[currentPage].setTextColor(getResources().getColor(R.color.colorPrimary));
}
将其添加到可绘制文件夹中。并将其设置为非活动点的背景
circle\u background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#78d9ff"/>
</shape>
编辑:
在上面的教程中,作者解释了如何使用viewpager实现Intro ImageSlider。你也可以看到他是如何实现dots布局的。
我已经实现了使用这个作为参考。希望有帮助。感谢@okcomputer\u kid,我使用以下代码来解决问题: active_dot_slider.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="@color/dotHome_active" />
</shape>
transparent_circle.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="#ffffff" />
</shape>
最后,展示点方法:
private void showDots(int pageNumber){
TextView [] dots = new TextView[viewPager.getAdapter().getCount()];
layoutDots.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(getContext());
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(30);
dots[i].setTextColor(Color.TRANSPARENT);
dots[i].setBackground((i!=pageNumber)?
getResources().getDrawable(R.drawable.transparent_circle):getResources().getDrawable(R.drawable.active_dot_slider));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(5, 5, 5, 5);
dots[i].setLayoutParams(params);
layoutDots.addView(dots[i]);
}
}
private void showDots(int页码){
TextView[]dots=newtextView[viewPager.getAdapter().getCount()];
layoutDots.removeAllViews();
对于(int i=0;i
感谢@okcomputer\u kid,我使用以下代码来解决问题:
active_dot_slider.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="@color/dotHome_active" />
</shape>
transparent_circle.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="#ffffff" />
</shape>
最后,展示点方法:
private void showDots(int pageNumber){
TextView [] dots = new TextView[viewPager.getAdapter().getCount()];
layoutDots.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(getContext());
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(30);
dots[i].setTextColor(Color.TRANSPARENT);
dots[i].setBackground((i!=pageNumber)?
getResources().getDrawable(R.drawable.transparent_circle):getResources().getDrawable(R.drawable.active_dot_slider));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(5, 5, 5, 5);
dots[i].setLayoutParams(params);
layoutDots.addView(dots[i]);
}
}
private void showDots(int页码){
TextView[]dots=newtextView[viewPager.getAdapter().getCount()];
layoutDots.removeAllViews();
对于(int i=0;i
此代码使用非活动颜色或活动颜色绘制点,但我希望非活动点透明且有白色边框您希望点透明且有白色边框吗?你的意思是让他们看不见?你到底想让你的活动点和非活动点怎么样?背景色、边框颜色和透明度(alpha)值我添加了我想提问的图片我已经编辑了我的答案。将circle_background.xml添加到drawable文件夹。我也更新了addButtons功能。试试这个代码用非活动的颜色或活动的颜色画一个点,但是我希望非活动的点是透明的并且有白色的边框你希望点是透明的并且有白色的边框吗?你的意思是让他们看不见?你到底想让你的活动点和非活动点怎么样?