在Android的GUI元素的动态数?元素、动态、Android、GUI

2023-09-08 08:38:07 作者:被抛弃的拳头

我想创建为Android GUI应用程序,用户可以添加或删除某些类型(4种不同类型的字段)的应用领域。有没有办法在XML这样做呢?

我能想出这样做的唯一方法是通过这听起来对我来说是一个坏主意的应用程序内edditing XML文件。

希望我的问题是清楚的。

Yotam。

编辑:

我添加了一个简单的code直接的java植入:

进口android.app.Activity;进口android.graphics.Color;进口android.os.Bundle;进口android.view.ViewGroup;进口android.widget.TextView;

 公共类莱昂尼达斯延伸活动{    / **当第一次创建活动调用。 * /    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        //setContentView(R.layout.counter);        TextView的电视=新的TextView(本);        TextView的UV =新的TextView(本);        TV.setText(你好);        UV.setText(偷懒);        //的setContentView(电视);        //的setContentView(UV);        ViewGroup.LayoutParams的LPAR =新ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.FILL_PARENT);        this.addContentView(UV,逻辑分区);        this.addContentView(电视,逻辑分区);        this.setVisible(真);    }} 

EDIT2:

我已经寻找榜样,得到了以下工作:

  LayoutInflater吹气;@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.main);    吹气= LayoutInflater.from(本);    按钮B =(按钮)this.findViewById(R.id.alert);    b.setOnClickListener(本);}@覆盖公共无效的onClick(视图v){    最终的LinearLayout帆布=(的LinearLayout)Leonidas.this.findViewById(R.id.counter_field);    最后查看CV = this.inflater.inflate(R.layout.counter,帆布,FALSE);    canvas.addView(CV);} 

解决方案 Android GUI框架及代码分析

您可以从您的处理程序中做到这一点(在实现类)。

充气你的XML布局后,你对某种类型的用户交互的响应。在处理程序中,你

无论是创建一个新的View划伤,并指定其的LayoutParams ,或使用XML膨胀有一个

具有新的视图后,将其添加到电流()视图,并且由于其的LayoutParams,这将是的大小,形状,颜色等。你想要的。

更新:

如果您想更复杂的视图添加到您的活动,最好把它们写在XML中,和膨胀他们:

sample_component.xml://内RES /布局

 <?XML版本=1.0编码=UTF-8&GT?;<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:方向=垂直的android:layout_width =FILL_PARENT    机器人:layout_height =WRAP_CONTENT机器人:填充=0像素>    < TextView的机器人:ID =@ + ID / servicename_status机器人:paddingLeft =15像素        机器人:paddingRight =5像素        机器人:文字样式=大胆的机器人:可聚焦=false的机器人:TEXTSIZE =14px的        机器人:layout_marginLeft =10px的机器人:layout_marginRight =的3px        机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT/>    < TextView的机器人:ID =@ + ID / lastcheck机器人:可聚焦=假        机器人:TEXTSIZE =14px的机器人:layout_width =FILL_PARENT        机器人:layout_marginLeft =10px的机器人:layout_marginRight =的3px        机器人:layout_height =WRAP_CONTENT机器人:layout_below =@ ID / servicename_status/>    < TextView的机器人:ID =@ + ID /时间机器人:可聚焦=假        机器人:TEXTSIZE =14px的机器人:layout_width =FILL_PARENT        机器人:layout_marginLeft =10px的机器人:layout_marginRight =的3px        机器人:layout_height =WRAP_CONTENT机器人:layout_below =@ ID / lastcheck/>    < TextView的机器人:ID =@ + ID /企图机器人:可聚焦=假        机器人:TEXTSIZE =14px的机器人:layout_width =FILL_PARENT        机器人:layout_marginLeft =10px的机器人:layout_marginRight =的3px        机器人:layout_height =WRAP_CONTENT机器人:layout_below =@ ID /持续时间/>    < TextView的机器人:ID =@ + ID / statusinfo机器人:可聚焦=假        机器人:TEXTSIZE =14px的机器人:layout_width =FILL_PARENT        机器人:layout_marginLeft =10px的机器人:layout_marginRight =的3px        机器人:layout_height =WRAP_CONTENT机器人:layout_below =@ ID /企图/>    <复选框机器人:ID =@ + ID /警报机器人:可聚焦=假        机器人:layout_alignParentRight =真正的机器人:freezesText =假        机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT        机器人:layout_marginTop =5像素/>< / RelativeLayout的> 

里面的莱昂尼达斯活动类,你有必须通过从视图添加/删除项目/不同的响应用户操作的处理程序。下面是一个click事件,它使用的样本处理程序 LayoutInflater ,到 sample_component.xml 视图添加到您的活动

 公共final类MyClickListener实现View.OnClickListener{    私人LayoutInflater吹气;    公共MyClickListener()    {        吹气= LayoutInflater.from(莱昂尼达斯。本);    }    @覆盖    公共无效的onClick(视图v)    {        // TODO:在此处更改的RelativeLayout到任何布局        //你想新的组件添加到        最终的RelativeLayout帆布=(RelativeLayout的)Leonidas.this.findViewById(R.id.my_canvas);        最后查看childView = inflater.inflate(R.layout.sample_component,帆布,FALSE);        // TODO:中查找addView方法的5个不同的签名,        //并挑选最适合您的需求        canvas.addView(childView);        //检查哪个按钮是pressed        开关(view.getId())        {            案例R.id.btn_ preV:                //处理程序$ P $光伏按钮                打破;            案例R.id.btn_next:                //处理程序下一个按钮                打破;            默认:                打破;        }    }} 

请注意,这MyClickListener被实现为您的莱昂尼达斯活动中内嵌类,大公就是为什么对于背景参数它被用于: this.Leonidas

更新

该R.id.my_canvas将要添加组件到视图的ID。它在你的main.xml中(或任何XML用于您的莱昂尼达斯视图)。

如果你把MyClickListener类的Leonidas.java类中(声明为内联类),它可以识别它。

I want to create a gui application for android where the user will be able to add or remove fields of certain type (4 different type of fields) to the application. Is there a way to do so in xml?

The only way I could figure to do so is by edditing the xml file from within the app which sounds as a bad idea for me.

Hope my question is clear.

Yotam.

Edit:

I have added a simple code for direct java implantation:

import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.ViewGroup; import 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);
    }
}

Edit2:

I have searched for example and got the following working:

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);
}

解决方案

You can do it from within your handler too (in the implementation class).

After inflating your xml layout, you respond to some kind of user interactions. In the handler you

either create a new View from scratch, and specify its layoutparams, or inflate one using xml

After having the new view, you add it to the current (this) view, and due to its layoutparams, it will be the size, shape, color, etc. that you want.

Update:

If you'd like to add more complex views to your activity, it's better to write them in xml, and inflate them:

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>

Inside your Leonidas activity class you have the handlers that have to respond to different user actions by adding/removing items to/from the view. Below is a sample handler of a click event, which uses LayoutInflater, to add the sample_component.xml view to your activity:

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;
        }
    }
}

Note, that MyClickListener is implemented as an inline class within your Leonidas activity, thay's why for the context parameter it is used: this.Leonidas.

Update

The R.id.my_canvas would be the id of the view that you want to add components to. it is in your main.xml (or whatever xml you use for your Leonidas view).

If you put the MyClickListener class inside your Leonidas.java class (declare as inline class), it will recognize it.