harmonyos鸿蒙,HarmonyOS鸿蒙之设置圆角

1、同时设置四个角

a、静态设置

graphic下新建 background_supertext_red.xml

ohos:shape="rectangle">

ohos:color="$color:white"/>

ohos:width="2"

ohos:color="$color:red"/>

ohos:radius="20"/>

layout.xml中引用

ohos:height="50vp"

ohos:width="match_parent"

ohos:background_element="$graphic:background_supertext_red"

ohos:margin="20vp"

ohos:text="按钮"

ohos:text_size="20fp"/>

b、动态设置,不在layout.xml中通过ohos:background_element引用,通过代码动态设置

Button button = (Button) findComponentById(ResourceTable.Id_btn);

button.setBackGround(ResourceTable.Graphic_background_supertext_red);

或者

Button button = (Button) findComponentById(ResourceTable.Id_btn);

ShapeElement element = new ShapeElement(getContext(),

ResourceTable.Graphic_background_supertext_red);

button.setBackground(element);

2、单独设置四个角中的某个圆角

element.setCornerRadiiArray(new float[]{

tlRadius, tlRadius,

trRadius, trRadius,

brRadius, brRadius,

blRadius, blRadius

});

举个例子吧(设置左上与右上圆角):

ohos:height="50vp"

ohos:width="match_parent"

ohos:background_element="$graphic:background_supertext_red"

ohos:margin="20vp"

ohos:text="按钮"

ohos:text_size="20fp"/>

background_supertext_red.xml中不在使用corners圆角属性

ohos:shape="rectangle">

ohos:color="$color:white"/>

ohos:width="2"

ohos:color="$color:red"/>

然后通过代码动态设置单独圆角,比如上圆角

Button button = (Button) findComponentById(ResourceTable.Id_btn);

ShapeElement element = new ShapeElement(getContext(),

ResourceTable.Graphic_background_supertext_red);

element.setCornerRadiiArray(new float[]{

20f, 20f,

20f, 20f,

0f, 0f,

0f, 0f

});

button.setBackground(element);

1、同时设置四个角 a、静态设置 graphic下新建 background_supertext_red.xml ohos:shape="rectangle"> ohos:color="$color:white"/> ohos:width="2" ohos:color="$color:red"/> ohos:radius="20"/> layout.xml中引用 ohos:height="50vp" ohos:width="match_parent" ohos:background_element="$graphic:background_supertext_red" ohos:margin="20vp" ohos:text="按钮" ohos:text_size="20fp"/> b、动态设置,不在layout.xml中通过ohos:background_element引用,通过代码动态设置 Button button = (Button) findComponentById(ResourceTable.Id_btn); button.setBackGround(ResourceTable.Graphic_background_supertext_red); 或者 Button button = (Button) findComponentById(ResourceTable.Id_btn); ShapeElement element = new ShapeElement(getContext(), ResourceTable.Graphic_background_supertext_red); button.setBackground(element); 2、单独设置四个角中的某个圆角 element.setCornerRadiiArray(new float[]{ tlRadius, tlRadius, trRadius, trRadius, brRadius, brRadius, blRadius, blRadius }); 举个例子吧(设置左上与右上圆角): ohos:height="50vp" ohos:width="match_parent" ohos:background_element="$graphic:background_supertext_red" ohos:margin="20vp" ohos:text="按钮" ohos:text_size="20fp"/> background_supertext_red.xml中不在使用corners圆角属性 ohos:shape="rectangle"> ohos:color="$color:white"/> ohos:width="2" ohos:color="$color:red"/> 然后通过代码动态设置单独圆角,比如上圆角 Button button = (Button) findComponentById(ResourceTable.Id_btn); ShapeElement element = new ShapeElement(getContext(), ResourceTable.Graphic_background_supertext_red); element.setCornerRadiiArray(new float[]{ 20f, 20f, 20f, 20f, 0f, 0f, 0f, 0f }); button.setBackground(element);
经验分享 程序员 微信小程序 职场和发展