在TextView中绘制圆圈

时间:2014-12-03 11:57:55

标签: android textview draw geometry

我的日历看起来像这样:

Calendar

绘制此Calendar的类称为MonthView,是TableLayout:

class MonthView extends TableLayout{...}

创建日历的功能如下所示:

TextView btn;
TableRow tr;

void DisplayMonth(boolean animationEnabled){
    ...

    for(int i = 0; i < 6; i++){
        if(day > cal.getActualMaximum(Calendar.DAY_OF_MONTH))
            break;
        tr = new TableRow(context);
        tr.setWeightSum(0.7f);

        for(int j = 0; j < 7; j++){
            btn = new TextView(context);
            ...
        }
}

更改背景日颜色的功能如下:

public void changeBackgroundDay(List<Class> classes){
    for(int i = 0; i < getChildCount; i++){
        TableRow row = (TableRow)getChildAt(i);

        for(int j = 0; j < row.getChildCount(); j++){
            TextView t = (TextView)row.getChildAt(j);

            for(int tam = 0; tam < classes.size(); tam++){
                Class class = classes.get(tam);

                // ---- THIS IS THE PART THAT I WANT TO CHANGE ----//
                if(class.getTypePlanning().equals("null"){
                    t.setBackgroundColor(Color.parseColor("#777777"); 
                }
                else if(class.getTypePlanning().equals("R")){
                    t.setBackgroundColor(Color.parseColor("#2477B2");
                }
                // -----------------------------------------------//
                ...
            }
        }
    }
}

我想,而不是仅仅更改TextView的背景颜色,在此TextView上绘制4个圆圈。在某些情况下,它可以只绘制为2或3.例如,如图所示:

Calendar with circles

enter image description here

我尝试使用包含形状和颜色的项目制作图层列表,并将此图层列表定义为TextView的背景,但它并没有很好地工作。

图层列表xml如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layerlist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <item android:id="@+id/itemnaoplanejado"
        android:bottom="1dp" android:right="1dp">
        <shape android:shape="oval" android:visible="false">
            <size android:width="2dp"
                android:height="2dp"/>
            <solid android:color="#777777"/>
        </shape>
    </item>

    <item android:id="@+id/itemplanejado"
        android:bottom="1dp" android:left="1dp">
        <shape android:shape="oval" android:visible="false">
            <size android:width="2dp"
                android:height="2dp"/>
            <solid android:color="#8CC9F3"/>
        </shape>
    </item>

    ....
</layer-list>

在函数changeBackgroundDay上我这样做:

public void changeBackgroundDay(List<Class> classes){
    ...
    if(class.getTypePlanning().equals("null"){
        LayerDrawable layerDrawable = (LayerDrawable) getResources().getDrawable(R.drawable.layerlistday);
        layerDrawable.findDrawableByLayerId(R.id.itemnotplanned).setVisible(true, false);

        t.setBackground(layerDrawable);
    }
}

有什么想法吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

Tyr this

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

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/num_txt"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginTop="0dp"
            android:background="@drawable/bg_red"
            android:gravity="center"
            android:text="My name is NON"
            android:textColor="#FFFFFF"
            android:textSize="10dp" />

    </RelativeLayout>

</RelativeLayout>

保存在drawable bg_red.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <corners android:radius="10dip"/>
    <stroke android:color="#FF0000" android:width="5dip"/>
    <solid android:color="#FF0000"/>
</shape>

编辑代码-----

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >
     <TextView
         android:id="@+id/num_txt"
         android:layout_width="185dp"
         android:layout_height="185dp"

         android:layout_alignParentTop="true"
         android:layout_marginTop="163dp"
         android:background="@drawable/bg_red"
         android:gravity="center"
         android:text="My name is NON"
         android:textColor="#FFFFFF"
         android:layout_marginLeft="10dp"
         android:textSize="10dp" />

     <TextView
         android:id="@+id/TextView02"
         android:layout_width="90dp"
         android:layout_height="90dp"
         android:layout_alignParentRight="true"
         android:layout_alignTop="@+id/TextView01"
         android:layout_marginRight="90dp"
         android:layout_marginTop="122dp"
         android:background="@drawable/bg_red"
         android:gravity="center"
         android:text="My name is NON"
         android:textColor="#FFFFFF"
         android:textSize="10dp" />

     <TextView
         android:id="@+id/TextView01"
         android:layout_width="120dp"
         android:layout_height="120dp"
         android:layout_alignTop="@+id/num_txt"
         android:layout_toRightOf="@+id/num_txt"
         android:background="@drawable/bg_red"
         android:gravity="center"
         android:text="My name is NON"
         android:textColor="#FFFFFF"
         android:textSize="10dp" />

</RelativeLayout>