【问题标题】:Draw circles inside TextView在 TextView 内绘制圆圈
【发布时间】:2015-01-31 23:39:49
【问题描述】:

我有一个如下所示的日历:

绘制这个Calendar的Class叫做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 上绘制 4 个圆圈,而不是仅仅改变 TextView 的背景颜色。在某些情况下,可以只画2个或3个。例如,如图所示:

我尝试制作一个包含形状和颜色的项目的图层列表,并将此图层列表定义为 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);
    }
}

有什么想法吗?

提前致谢。

【问题讨论】:

  • 你是如何将 LayerList 应用到 TextView 的?
  • 你为什么不使用网格视图呢,你可以用 GridView 做很多自定义。
  • 我将编辑我的答案并放入 LayerList 实现
  • @AshishTamrakar 是的,这可能是最好的选择,但如果有任何方法可以使用 TableLayout 来做到这一点,那就太好了......如果没有,我将更改为 GridView
  • 使用 setcompounddrawables 代替背景

标签: android textview draw geometry


【解决方案1】:

提这个

<?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>

保存在 drwable 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>

【讨论】:

  • 如果我有一个静态 xml,它会工作,但我是动态的。
猜你喜欢
  • 1970-01-01
  • 2019-01-17
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 2021-10-15
  • 2015-01-05
  • 1970-01-01
  • 2014-04-11
相关资源
最近更新 更多