Android Runtime Layout Tutorial

后端 未结 4 1920
灰色年华
灰色年华 2020-11-29 05:04

Does anyone know how to perform or have a good reference for doing an activity layout at runtime in android?

Here is the code for my activity. I\'m sure I\'m just ne

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 05:24

    You can check out this URL: http://www.linux-mag.com/cache/7705/1.html . It has both library widgets and custom widgets.

    EDIT:

    setBackgroundColor requires input in proper ARGB format: 0xAARRGGBB. Each AA, RR, GG and BB range from 00 (minimum) to ff (maximum).

    The bare minimum example goes here and it works flawlessly. Here are the screenshot and code (modified a bit):

    http://picturepush.com/public/3313522 (old)

    package us.simpleit;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class SimpleGUI extends Activity {
        TextView tv;
        EditText et;
        LinearLayout ll;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            //LinearLayout ll = new LinearLayout(this);
            ll = new LinearLayout(this);
            ll.setOrientation(android.widget.LinearLayout.VERTICAL);
            ll.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
            // ARGB: Opaque Red
            ll.setBackgroundColor(0x88ff0000);
    
            tv = new TextView(this);
            tv.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
            tv.setText("sample text goes here");
            // ARGB: Opaque Green
            tv.setBackgroundColor(0x5500ff00);
            ll.addView(tv);
    
            et = new EditText(this);
            et.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
            et.setText("edit me please");
            // ARGB: Solid Blue
            et.setBackgroundColor(0xff0000ff);
            ll.addView(et);
    
            Button btn = new Button(this);
            btn.setText("Go!");
            btn.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    tv.setText(et.getText().toString());
                }
            });
    
            ll.addView(btn);
            setContentView(ll);
    
            //setContentView(R.layout.main);
        }
    }
    

提交回复
热议问题