开发者

dynamic number of gui elements in Android?

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)Leon开发者_JS百科idas.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.


Instead of specifying elements in the XML, you can create them dynamically and add it to the UI. This is demonstrated in the Android Hello World Tutorial here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜