Monday, September 10, 2012

Relative Layout + custom view (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CustomView v = new CustomView(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100,100);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        //v.setLayoutParams(lp2);
        v.setBackgroundColor(Color.parseColor("#333333"));
        
        CustomView v2 = new CustomView(this);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(100,100);
        lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        //v.setLayoutParams(lp2);
        v2.setBackgroundColor(Color.parseColor("#333333"));

        RelativeLayout relativeLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams _lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT);
        //relativeLayout.setLayoutParams(lp);
        relativeLayout.addView(v,lp);
        relativeLayout.addView(v2,lp2);
        
        setContentView(relativeLayout,_lp);
    }
}

class CustomView extends View
{  
 private Paint paint;
 
 public CustomView(Context context) {  
  super(context); 
  paint = new Paint();
  paint.setColor(Color.WHITE);
  paint.setTextSize(18);
  paint.setAntiAlias(true);
 }  
 protected void onDraw(Canvas canvas) {  
 // TODO Auto-generated method stub  
  super.onDraw(canvas);  
 
  canvas.drawText("Hello World", 5, 30, paint);
  invalidate();
 }
}  



No comments:

Post a Comment