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组件如何设置渐变背景
2589浏览 • 1回复 待解决
HarmonyOS Button 无法添加margin left right
276浏览 • 1回复 待解决
如何为图片添加一个模糊的效果?
479浏览 • 2回复 待解决