Android的,动态变化的看法,添加/删除新的组件 [英] Android, dynamically changing the view, adding/removing new components

查看:226
本文介绍了Android的,动态变化的看法,添加/删除新的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个机器人新手,所以原谅我,如果这个问题很简单。

I am an android newbie so forgive me if this question is simple.

基本上我想在端口一个Java项目到Android。在我的Java应用程序视图组件动态地根据型号的不同变化。即该模型是显示器需要n检查取决于枚举类型箱或N单选按钮被传递到视图。

Basically I want to port over a java project to Android. By view components in my java application dynamically change depending on the model. I.e the model being display may require n check boxes or n radio boxes depending on the enum TYPE being passed to the view.

在我的Java显示,这是通过嵌套的循环与n号每次加组件到屏幕实现。例如在code的简化版本:

In my java display this is achieved by nested loops with the n number each time adding the component to the screen. e.g a simplified version of the code:

if (type == TYPE.CHECKBOXES){
for (int i = 0; i < n ; i++){
add(new JCheckBox()); 
}}

这是针对视图每次下一个按钮为pressed(类型每次改变)处理。在Android中如何动态地添加和删除组件,或者我需要设置为每种类型的一个新的看法?

This is processed for the view each time a next button is pressed (the type changes each time). In android how do i dynamically add and remove components, or do i need to set up a new view for each type?

非常感谢
山姆

Many thanks Sam

推荐答案

在Java中,你使用管理布局的JP​​anel 非常久远的具体布局(即 BorderLayout的的FlowLayout 等)
您添加组件的JP​​anel 然后添加的JP​​anel 母公司或根布局。

In Java you manage your layouts using JPanel alongwith specific layouts (i.e BorderLayout, FlowLayout etc.) you add your components to JPanel and then add your JPanel to parent or root layout.

一个小例子(如你贴:)

a little example (as you posted:)

if (type == TYPE.CHECKBOXES){
     for (int i = 0; i < n ; i++){
          mayPanel.add(new JCheckBox()); 
     }
}

在安卓您管理使用的ViewGroup 您的布局,其中有具体的子类的布局设计(即的LinearLayout 就像为的FlowLayout RelativeLayout的可被视为的BorderLayout

In android you manage your layouts using ViewGroup which has specific subclassed layout design (i.e LinearLayout much like as FlowLayout, RelativeLayout may be considered BorderLayout)

您可以使用这些布局就像一个的JP​​anel

You can use these layouts just like a JPanel

if (type == TYPE.CHECKBOXES){
     LinearLayout myPanel = new LinearLayout(this);
     for (int i = 0; i < n ; i++){
          CheckBox chk=new CheckBox(this);  
          chk.setText("Hello");
          myPanel.addView(chk); 
     }
     myRootPanel.addView(myPanel);
}

只是一个加​​法:在组件用Java在Android中被称为查看。大部分的控制(也称为像按钮的EditText 的ImageButton 的ImageView )的Andr​​oid的是查看子类

Just an addition: The Component in Java are called View in Android. Most of the control (also called widgets like Button, EditText, ImageButton, ImageView) in Android are subclass of View.

这篇关于Android的,动态变化的看法,添加/删除新的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆