Dynamic user interface creation problem
How to Create dynamic user interface with events...
Hi Friends i want to create a user interface开发者_如何学编程. in which i will have some buttons and labels but the number of buttons and lables will be changing time to time depending upon the data retrieved from the server...
Can any one please guide me to do solve this issue... with usefuls links and guidence..
This uses two linear layouts to achieve a grid. The higher level LinearLayout is defined in linearlayout.xml, and holds element views vertically. Within each of these vertical views, a Row
(custom LinearLayout) is instanciated, and in doing so, specifies the event handler(s). This LinearLayout holds its elements horizontally.
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.linearlayout);
ViewGroup main = (ViewGroup) findViewById(R.id.linearlayout);
main.addView(new Row(this));
main.addView(new Row(this));
}
private class Row extends LinearLayout {
public Row(Context context) {
super(context);
TextView text = new TextView(context);
Button button = new Button(context);
text.setText("Text");
button.setText("Button");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
this.addView(text);
this.addView(button);
}
}
}
linearlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
Is this what you were looking for?
精彩评论