动态数量的gui元素在Android中?

如何解决动态数量的gui元素在Android中?

| 我想为Android创建一个gui应用程序,其中用户将能够向该应用程序添加或删除某些类型的字段(4种不同类型的字段)。有没有办法在xml中这样做? 我唯一能想到的方法是从应用程序内编辑xml文件,这听起来对我来说不是一个好主意。 希望我的问题清楚。 Yotam。 编辑: 我为直接的Java植入添加了一个简单的代码: 导入android.app.Activity; 导入android.graphics.Color; 导入android.os.Bundle; 导入android.view.ViewGroup; 导入android.widget.TextView;
public class Leonidas extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.counter);
        TextView TV = new TextView (this);
        TextView UV = new TextView (this);
        TV.setText(\"hello\");
        UV.setText(\"goof\");
        //setContentView(TV);
        //setContentView(UV);
        ViewGroup.LayoutParams lpars = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
        this.addContentView(UV,lpars);
        this.addContentView(TV,lpars);
        this.setVisible(true);
    }
}
编辑2: 我搜索了例如,并得到以下工作:
LayoutInflater inflater;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    inflater = LayoutInflater.from(this);
    Button b = (Button) this.findViewById(R.id.alert);
    b.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    final LinearLayout canvas = (LinearLayout)Leonidas.this.findViewById(R.id.counter_field);
    final View cv = this.inflater.inflate(R.layout.counter,canvas,false);
    canvas.addView(cv);
}
    

解决方法

您也可以在处理程序内部(在实现类中)进行操作。 扩展xml布局后,您将响应某种类型的用户交互。 在处理程序中 从中创建一个新的视图 从头开始,并指定其
layoutparams
, 或使用xml充气 拥有新视图后,将其添加到当前(
this
)视图中,由于其layoutparams,它将是您想要的大小,形状,颜色等。 更新: 如果您想为您的活动添加更复杂的视图,最好以xml形式编写它们并对其进行充气: sample_component.xml://内部res / layout
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:orientation=\"vertical\" android:layout_width=\"fill_parent\" 
    android:layout_height=\"wrap_content\" android:padding=\"0px\">
    <TextView android:id=\"@+id/servicename_status\" android:paddingLeft=\"15px\" 
        android:paddingRight=\"5px\"
        android:textStyle=\"bold\" android:focusable=\"false\" android:textSize=\"14px\"
        android:layout_marginLeft=\"10px\" android:layout_marginRight=\"3px\" 
        android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\" />
    <TextView android:id=\"@+id/lastcheck\" android:focusable=\"false\"
        android:textSize=\"14px\" android:layout_width=\"fill_parent\"
        android:layout_marginLeft=\"10px\" android:layout_marginRight=\"3px\" 
        android:layout_height=\"wrap_content\" android:layout_below=\"@id/servicename_status\" />
    <TextView android:id=\"@+id/duration\" android:focusable=\"false\"
        android:textSize=\"14px\" android:layout_width=\"fill_parent\"
        android:layout_marginLeft=\"10px\" android:layout_marginRight=\"3px\" 
        android:layout_height=\"wrap_content\" android:layout_below=\"@id/lastcheck\" />
    <TextView android:id=\"@+id/attempt\" android:focusable=\"false\"
        android:textSize=\"14px\" android:layout_width=\"fill_parent\"
        android:layout_marginLeft=\"10px\" android:layout_marginRight=\"3px\" 
        android:layout_height=\"wrap_content\" android:layout_below=\"@id/duration\" />
    <TextView android:id=\"@+id/statusinfo\" android:focusable=\"false\"
        android:textSize=\"14px\" android:layout_width=\"fill_parent\"
        android:layout_marginLeft=\"10px\" android:layout_marginRight=\"3px\" 
        android:layout_height=\"wrap_content\" android:layout_below=\"@id/attempt\" />
    <CheckBox android:id=\"@+id/alert\" android:focusable=\"false\" 
        android:layout_alignParentRight=\"true\" android:freezesText=\"false\"
        android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" 
        android:layout_marginTop=\"5px\" />
</RelativeLayout>
在“ 5”活动类中,处理程序必须通过在视图中添加/删除项目来响应不同的用户操作。 以下是click事件的示例处理程序,该事件使用
LayoutInflater
sample_component.xml
视图添加到您的活动中:
public final class MyClickListener implements View.OnClickListener
{
    private LayoutInflater inflater;

    public MyClickListener()
    {
        inflater = LayoutInflater.from(Leonidas .this);
    }

    @Override
    public void onClick(View v)
    {
        //  TODO: change RelativeLayout here to whatever layout 
        //  you\'d like to add the new components to
        final RelativeLayout canvas = (RelativeLayout)Leonidas.this.findViewById(R.id.my_canvas);
        final View childView = inflater.inflate(R.layout.sample_component,canvas,false);
        //  TODO: Look up the 5 different signatures of the addView method,//  and pick that best fits your needs
        canvas.addView(childView);

        // check which button was pressed
        switch (view.getId())
        {
            case R.id.btn_prev:
                //handler for the prev button
                break;
            case R.id.btn_next:
                //handler for the next button
                break;
            default:
                break;
        }
    }
}
注意,MyClickListener是作为Leonidas活动中的内联类实现的,这就是为什么使用
context
参数的原因::10ѭ。 更新资料 R.id.my_canvas将是您要向其中添加组件的视图的ID。它位于main.xml(或用于Leonidas视图的任何xml)中。 如果将MyClickListener类放入Leonidas.java类(声明为内联类)中,它将识别出它。     ,无需在XML中指定元素,而是可以动态创建它们并将其添加到UI。在此处的Android Hello World教程中对此进行了演示。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-