在android中只填写视图背景的x%?

时间:2016-07-21 05:44:03

标签: android

在android中,我想用一个像黑色的颜色来填充视图的一部分背景(例如,右边或左边的70%)。 我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您可以使用(形状,渐变)来实现此效果。 Android设置的背景颜色可以在xml res / drawable中定义, 如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:angle="0"
        android:endColor="#000"
        android:startColor="#FFF" />
</shape>

Shape用于定义形状,渐变内部定义渐变填充的形状,startColor起始颜色,endColor结束颜色,角度指示方向角度。当角度= 0时,渐变从左到右。

然后设置形状以查看背景。

android:background="@drawable/background"

答案 1 :(得分:0)

只需创建一个10px宽@ 1px高的BitmapDrawable,用Color.TRANSPARENT填充它,然后使用所需的颜色从0,0到x,0画一条线。使用Canvas来实现这一切。

然后调用yourcontrol.setBackgroundDrawable(bitmap)你已经完成了