Java 在android studio中创建固定大小的圆形视图
所以我有一个让我有点沮丧的问题。我想要实现的是一个按钮,它的中心有两个文本视图。一个有名字,一个有数字值。。。就像一个柜台 我希望它看起来像这样: 有一个薄的白色边框围绕着它(鉴于它的颜色是不完全可见的…对不起) 我想做的是根据按钮的按下方式来增加和减少值 到目前为止,我有以下几点: attrs.xmlJava 在android studio中创建固定大小的圆形视图,java,android,xml,android-studio,android-view,Java,Android,Xml,Android Studio,Android View,所以我有一个让我有点沮丧的问题。我想要实现的是一个按钮,它的中心有两个文本视图。一个有名字,一个有数字值。。。就像一个柜台 我希望它看起来像这样: 有一个薄的白色边框围绕着它(鉴于它的颜色是不完全可见的…对不起) 我想做的是根据按钮的按下方式来增加和减少值 到目前为止,我有以下几点: attrs.xml <resources> <declare-styleable name="ButtonCounter"> <attr name="backgroundCo
<resources>
<declare-styleable name="ButtonCounter">
<attr name="backgroundColor" format="color"/>
<attr name="borderColor" format="color"/>
<attr name="borderSize" format="integer"/>
<attr name="labelNameColor" format="color"/>
<attr name="labelValueColor" format="color"/>
<attr name="labelName" format="string"></attr>
<attr name="labelValue" format="string"></attr>
</declare-styleable>
}
这并没有达到我想要的效果,因为它变得太大了
关于我如何开始尝试这个有什么想法吗
谢谢 Hi如果希望在不使用Java类的情况下实现相同的功能,请参阅以下解决方案:
希望它能帮助你。
谢谢你用简单的方法试过了吗?创建
ShapeDrawable
xml?尝试使用drawable如何在布局中使用视图?我是否能够在Java代码中动态添加所有这些内容?对不起,我在凌晨3点写了这个问题,当时我已经睡了一半,所以忘了提一下,当用户需要计数器时,我需要添加多个计数器
public class ButtonCounter extends View {
private int backgroundColor,
borderColor,
borderSize,
labelNameColor,
labelValueColor;
private String labelName,
labelValue;
private Paint paintCircle,
paintStroke;
public ButtonCounter(Context context, AttributeSet attrs) {
super(context);
paintCircle = new Paint();
paintStroke = new Paint();
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.ButtonCounter
,
0,
0
);
try {
backgroundColor = a.getInteger(R.styleable.ButtonCounter_backgroundColor, 0);
borderColor = a.getInteger(R.styleable.ButtonCounter_borderColor, 0);
borderSize = a.getInteger(R.styleable.ButtonCounter_borderSize, 0);
labelNameColor = a.getInteger(R.styleable.ButtonCounter_labelNameColor,0);
labelValueColor = a.getInteger(R.styleable.ButtonCounter_labelValueColor, 0 );
labelName = a.getString(R.styleable.ButtonCounter_labelName);
labelValue = a.getString(R.styleable.ButtonCounter_labelValue);
} finally {
a.recycle();
}
}
@Override
protected void onDraw(Canvas canvas) {
paintCircle.setColor(backgroundColor);
paintCircle.setAntiAlias(true);
paintStroke.setColor(borderColor);
paintStroke.setAntiAlias(true);
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
int diameter = ((height > width) ? height : width);
int radius = diameter / 2;
canvas.drawCircle(diameter / 2, diameter / 2, radius - borderSize, paintCircle);
canvas.drawCircle(diameter / 2, diameter / 2, radius, paintStroke);
}
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/black" />
</shape>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:background="@drawable/circle_bg">
<TextView
android:layout_width="wrap_content"
android:textColor="@color/white"
android:textSize="15sp"
android:layout_height="wrap_content"
android:text="Global"/>
<TextView
android:layout_width="wrap_content"
android:textColor="@color/white"
android:textSize="22sp"
android:layout_height="wrap_content"
android:text="0"/>
</LinearLayout>