如何解决动态数量的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 举报,一经查实,本站将立刻删除。