Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android studio中创建固定大小的圆形视图_Java_Android_Xml_Android Studio_Android View - Fatal编程技术网

Java 在android studio中创建固定大小的圆形视图

Java 在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

所以我有一个让我有点沮丧的问题。我想要实现的是一个按钮,它的中心有两个文本视图。一个有名字,一个有数字值。。。就像一个柜台

我希望它看起来像这样:

有一个薄的白色边框围绕着它(鉴于它的颜色是不完全可见的…对不起)

我想做的是根据按钮的按下方式来增加和减少值

到目前为止,我有以下几点:

attrs.xml

<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类的情况下实现相同的功能,请参阅以下解决方案:

  • 创建一个具有任意名称的可绘制文件,让我们圈出\u bg\u xml
  • 
    
  • 现在,在布局中,将两个文本视图(或任何视图)放在LinearLayout内,并将其设置为可绘制的布局背景
  • 
    
    希望它能帮助你。
    谢谢

    你用简单的方法试过了吗?创建
    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>