为了这效果肝了几天,调了几天,改了几天,头都晕了!
在 ViewBinder 里设置背景
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if (view.getId() == R.id.textView_count) {
Date date = new Date(cursor.getLong(columnIndex));
Date date1 = new Date();
long ms = date.getTime() - date1.getTime();
long d = ms / 1000 / 60 / 60 / 24;
if (ms >= 0)
view.setBackgroundColor(0xff00bfff);
else
view.setBackgroundColor(0xffffa500);
((TextView)view).setText(Math.abs(d) + "");
return true;
} else if (view.getId() == R.id.textView_day) {
Date date = new Date(cursor.getLong(columnIndex));
Date date1 = new Date();
long ms = date.getTime() - date1.getTime();
if (ms >= 0)
view.setBackgroundResource(R.drawable.day_bg);
else
view.setBackgroundResource(R.drawable.day_bg1);
return true;
}
return false;
}
});
半边圆角用 layer-list 做
[Android] Shape背景制作半圆或半边框
event_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="10dp" />
<solid android:color="#ffffff" />
</shape>
</item>
<item android:left="10dp">
<shape>
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
day_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="10dp" />
<solid android:color="#1e90ff" />
</shape>
</item>
<item android:right="10dp">
<shape>
<solid android:color="#1e90ff" />
</shape>
</item>
</layer-list>
day_bg1.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="10dp" />
<solid android:color="#ff6600" />
</shape>
</item>
<item android:right="10dp" >
<shape>
<solid android:color="#ff6600" />
</shape>
</item>
</layer-list>
ListView设置选中状态
ListView设置选中状态_listview 选中_ws_lm的博客-CSDN博客
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
item_activated.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/item_bg1"/>
<item android:drawable="@drawable/item_bg"/>
</selector>
item_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="10dp" />
<solid android:color="#f3f3f3" />
</shape>
</item>
</layer-list>
item_bg1.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="10dp" />
<solid android:color="#0000ff" />
</shape>
</item>
</layer-list>
item_event.xml
每一项用 margin 空出边缘显示背景
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/item_activated">
<TextView
android:id="@+id/textView_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="id"
android:visibility="gone" />
<TextView
android:id="@+id/textView_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/textView_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:background="@drawable/event_bg"
android:padding="5sp"
android:text="事件"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_count"
android:layout_width="60sp"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:gravity="center"
android:text="N"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_day"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:gravity="center"
android:paddingLeft="7sp"
android:paddingRight="7sp"
android:text="天"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
项目地址:GitLink | 确实开源