java代码如何为button添加xml背景?

java代码如何为button添加xml背景

鸿蒙
java
button
2022-04-29 15:12:50
浏览
收藏 0
回答 2
待解决
回答 2
按赞同
/
按时间
郑夏山
2

在代码中使用xml背景文件,我没找到响应代码;但是,对应的Java对象也能实现xml中的他特性:

        DirectionalLayout dl = (DirectionalLayout) findComponentById(ResourceTable.Id_id_dl); 

 
        Button btn = new Button(this); 

 
        //LayoutConfig(int width, int height, int alignment, float weight) 

        DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig( 

                600, 300 

        ); 

        config.alignment = LayoutAlignment.START; 

        btn.setLayoutConfig(config); 

 
        btn.setPadding(0, 0, 0, 0); 

        btn.setMarginLeft(20); 

        btn.setMarginTop(20); 

 
        btn.setText("点击"); 

        btn.setTextSize(60, Text.TextSizeType.VP); 

        btn.setTextAlignment(TextAlignment.CENTER); 

 
        //简单点:直接设置背景颜色 

        /* 

        ShapeElement shapeElement = new ShapeElement(); 

        shapeElement.setRgbColor(new RgbColor(255,255,0)); 

        btn.setBackground(shapeElement); 

         */ 

 
        //复杂点1 

        /* 

        // 创建正常状态下元素 

        ShapeElement normalShapeElement = new ShapeElement(); 

        normalShapeElement.setShape(ShapeElement.RECTANGLE); 

        normalShapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFff0000)); 

        normalShapeElement.setCornerRadius(btn.getHeight()/2.0f); 

        normalShapeElement.setStroke(20, RgbColor.fromArgbInt(0xff0000ff)); 

 
        // 创建按下状态下元素 

        ShapeElement pressedShapeElement = new ShapeElement(); 

        pressedShapeElement.setShape(ShapeElement.RECTANGLE); 

        pressedShapeElement.setRgbColor(RgbColor.fromArgbInt(0xFF00ff00)); 

        pressedShapeElement.setCornerRadius(btn.getHeight()/2.0f); 

        pressedShapeElement.setStroke(20, RgbColor.fromArgbInt(0xff0000ff)); 

 
        StateElement stateElement = new StateElement(); 

        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, pressedShapeElement); 

        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, normalShapeElement); 

        btn.setBackground(stateElement); 

        */ 

 
        //复杂点2 

        // 创建正常状态下元素 

        Resource normalResource = null; 

        try { 

            normalResource = this.getResourceManager().getResource(ResourceTable.Media_p1); 

        } catch (IOException e) { 

            e.printStackTrace(); 

        } catch (NotExistException e) { 

            e.printStackTrace(); 

        } 

        PixelMapElement normalElement = new PixelMapElement(normalResource); 

 
        // 创建按下状态下元素 

        Resource pressedResource = null; 

        try { 

            pressedResource = this.getResourceManager().getResource(ResourceTable.Media_p2); 

        } catch (IOException e) { 

            e.printStackTrace(); 

        } catch (NotExistException e) { 

            e.printStackTrace(); 

        } 

        PixelMapElement pressedElement = new PixelMapElement(pressedResource); 

 
        StateElement stateElement = new StateElement(); 

        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, pressedElement); 

        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, normalElement); 

        btn.setBackground(stateElement); 

         

        //添加到父窗口 

        dl.addComponent(btn); 
分享
微博
QQ
微信
回复
2022-04-29 16:28:18
代码自由

jsp????

分享
微博
QQ
微信
回复
2022-05-06 13:16:14
相关问题
Button组件如何设置渐变背景
244浏览 • 1回复 待解决
鸿蒙系统-代码如何实现添加联系人
8986浏览 • 1回复 待解决
如何为 C++ 提供回调函数?
512浏览 • 1回复 待解决
鸿蒙图片资源在Java代码如何使用
6237浏览 • 1回复 待解决
能使用ArkTS来调用Java代码吗?
3626浏览 • 1回复 待解决
XComponent组件如何设置背景颜色
291浏览 • 1回复 待解决
如何设置卡片背景为透明
480浏览 • 1回复 待解决
如何查看官方的xml文档?
8872浏览 • 2回复 待解决
如何设置窗口的背景
143浏览 • 1回复 待解决
java代码设置below的问题有懂的吗?
1257浏览 • 1回复 待解决
如何背景颜色设置为透明
220浏览 • 1回复 待解决