Android listview的背景颜色(百分比)
是否可以使用百分比因子为Listview元素的背景着色?例如,如果Listview中有3个元素,其中一个有2个点,另一个只有一个点: A:2分-->66.666% B:1分-->33.3333% C:0分-->0% 我能用某种方式给背景上色吗,像这样 A |------------------| B |----- C | |您可以尝试使用,并在列表适配器中设置Android listview的背景颜色(百分比),android,listview,colors,background,Android,Listview,Colors,Background,是否可以使用百分比因子为Listview元素的背景着色?例如,如果Listview中有3个元素,其中一个有2个点,另一个只有一个点: A:2分-->66.666% B:1分-->33.3333% C:0分-->0% 我能用某种方式给背景上色吗,像这样 A |------------------| B |----- C | |您可以尝试使用,并在列表适配器中设置getView()的可绘制内部的级别。我以这种方式解决: <?xml version="1.0" encoding="utf-8"?
getView()
的可绘制内部的级别。我以这种方式解决:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/voteBackground"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/gray_light"
>
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvVote"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/orange"
android:textStyle="bold" />
<TextView
android:id="@+id/tvVoteNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@color/orange"
android:padding="10dp"
android:text="dsadsadsadsadsa"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
为此,请使用自定义列表视图。您对此有何建议?
DisplayMetrics displayMetrics = mContext.getResources()
.getDisplayMetrics();
int screenWidthInPix=displayMetrics.widthPixels;
FrameLayout background = (FrameLayout)convertView.findViewById(R.id.voteBackground);
ViewGroup.LayoutParams params = background.getLayoutParams();
params.width = (screenWidthInPix*entry.getVote())/total;
background.setLayoutParams(params);