Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中gui元素的动态数量?_Android_User Interface_Android Layout - Fatal编程技术网

Android中gui元素的动态数量?

Android中gui元素的动态数量?,android,user-interface,android-layout,Android,User Interface,Android Layout,我想为android创建一个gui应用程序,用户可以在该应用程序中添加或删除特定类型的字段(4种不同类型的字段)。有没有一种在xml中这样做的方法 我能想到的唯一方法是从应用程序中编辑xml文件,这对我来说是个坏主意 希望我的问题清楚 约坦 编辑: 我为直接java植入添加了一个简单的代码: 导入android.app.Activity; 导入android.graphics.Color; 导入android.os.Bundle; 导入android.view.ViewGroup; 导入andr

我想为android创建一个gui应用程序,用户可以在该应用程序中添加或删除特定类型的字段(4种不同类型的字段)。有没有一种在xml中这样做的方法

我能想到的唯一方法是从应用程序中编辑xml文件,这对我来说是个坏主意

希望我的问题清楚

约坦

编辑:

我为直接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);
}

您可以动态创建元素并将其添加到UI,而不是在XML中指定元素。这在Android Hello World教程中得到了演示。

您也可以在处理程序中(在实现类中)执行此操作

在扩展xml布局之后,您将响应某种用户交互。 在处理程序中,您

  • 从中创建一个新视图 scratch,并指定其名称
    布局参数
  • 或者使用xml将其膨胀
拥有新视图后,将其添加到当前(
)视图中,由于其布局参数,它将是所需的大小、形状、颜色等

更新:

如果您想向活动中添加更复杂的视图,最好用xml编写它们,并将它们膨胀:

sample_component.xml://inside 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>
请注意,MyClickListener是在Leonidas活动中作为内联类实现的,这就是为什么要使用
上下文
参数:
this.Leonidas

更新

R.id.my_画布将是要向其中添加组件的视图的id。它位于main.xml(或用于Leonidas视图的任何xml)中


如果将MyClickListener类放入Leonidas.java类(声明为内联类),它将识别它

如果您有用XML定义的自定义视图,您可以使用视图充气器使用Java动态地创建它们。但是,我认为您甚至不可能在运行时编辑自己的XML文件。您几乎肯定必须用java创建这些视图,并将它们添加到布局中,而不是试图编辑布局xml文件。谢谢。我试图这样做,但我不知道如何使用addContent方法。我得到的是两个视图,一个在另一个上。我假设我需要以一种比我更“优雅”的方式设置参数。我在我的原始文本中附加了一个简单的代码。谢谢。我试图搜索xml充气机使用指南,但找不到一个对我来说很清楚的。我刚刚用充气机使用的一个小示例更新了这个答案,也许它会帮助你澄清一些事情。这有点帮助,但…(首先,你的行结束Leonidas.this.findViewByID(R.layout.my_canvas)对我或eclipse来说没有任何意义。我试图从自动完成中猜出一些东西,但没有成功。第二,当我尝试键入Leonidas时。eclipse的这一部分说,在范围内无法访问Leonidas类型的封闭实例。谢谢。我已经做了一些工作,但现在我有了一个新问题。我如何判断哪个按钮被禁用essed?我想使用一个单击侦听器来决定调用哪个方法。我假设这是代码中(视图v)部分的角色,但我可以用它做什么?谢谢您应该检查
View.getId()
。检查更新中的示例
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;
        }
    }
}