Android Drawable: Specifying shape width in percent in the XML file?

前端 未结 5 1498
攒了一身酷
攒了一身酷 2020-12-06 04:06

I\'m trying to create a simple Drawable that I want to set as the background for a view (using setBackgroundDrawable). I simply want to divide the background of the

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 04:54

    You can't specify a percentage. You need to specify a Dimension value to it.

    android:width is defined here: http://developer.android.com/reference/android/R.attr.html#width :

    (emphasis mine)

    Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

    For a definition of each dimension type, see: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

    You will need to create your own attribute (see styleable) and perform the calculations yourself in onDraw(...).

    See these two questions and the links therein for examples:

    • Defining custom attrs

    • Declaring styleable attributes in Android

提交回复
热议问题