如何在Android中设置java代码的角落

时间:2013-06-06 02:15:46

标签: android

很容易像这样设置XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BFEEFF" 
        android:endColor="#40B9FF" 
        android:angle="270"/>
    <corners 
        android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip"/>
</shape>

但我怎样才能在java代码中设置它?

2 个答案:

答案 0 :(得分:1)

通货膨胀之后,它不是一个可塑的形状(我知道这很令人困惑),
  相反,它是一个GradientDrawable,代码很简单,请查看文档http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html
特别是setCornerRadius()。

GradientDrawable mDrawable = new GradientDrawable();
mDrawable.setShape(GradientDrawable.RECTANGLE);
mDrawable.setCornerRadius(5.0f);
....
// (note, this is untested and you might need to set other properties,
// e.g. color etc.)

答案 1 :(得分:0)

我想你想在动态时创建一些View。 (如果这是真的)

我知道,相对于你的问题,它不是一个正确的答案,但我会给你一种在动态时间执行此操作的替代方法,在Selector文件中添加shape文件并在View的android:drawable =“@中添加选择器文件drawable / selector“。