【问题标题】:How to add a component like a button, programmatically from the java code instead of adding it from XML?如何从java代码以编程方式添加按钮等组件,而不是从XML添加它?
【发布时间】:2021-07-30 09:44:01
【问题描述】:

我想从 java 代码中以编程方式添加一个按钮,而不是从 XML 文件中添加它。我该怎么做?

【问题讨论】:

    标签: java android huawei-mobile-services huawei-developers harmonyos


    【解决方案1】:

    第一步是在onStart()中实例化按钮,稍后将实例化的按钮添加到ComponentContainer或setUIContent()中;

     @Override
        protected void onStart(Intent intent) {
            super.onStart(intent);
            Button button = new Button(MainAbilitySlice.this);
            setUIContent(button);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-05
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      相关资源
      最近更新 更多