【发布时间】: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