Three ways to get Linearinflater
A. Layoutinflater Inflater = Getlayoutinflater ();
B. layoutinflater Localinflater =
(Layoutinflater) Context.getsystemservice
(Context.layout_inflater_service);
C. layoutinflater Inflater = layoutinflater.from (context);
OnDraw method drawing, Invalidate refresh interface.
:
Click to change color
OnDraw after drawing, set the Click event to the control, upload the parameters to the control, and invalidate refresh
1.onDraw drawing, and adding ChangeColor methods
[Java]View Plaincopy
- Public class CUSVIEW3 extends View {
- private int color = 0;
- Public CUSVIEW3 (context context, AttributeSet attrs) {
- Super (context, attrs);
- }
- @Override
- protected void OnDraw (canvas canvas) {
- Super.ondraw (canvas);
- Paint Mpaint = new Paint ();
- if (Color > 2) {
- color = 0;
- }
- switch (color) {
- Case 0:
- Mpaint.setcolor (Color.green);
- Break ;
- case 1:
- Mpaint.setcolor (color.red);
- Break ;
- Case 2:
- Mpaint.setcolor (Color.Blue);
- Break ;
- Default:
- Break ;
- }
- Mpaint.setstyle (Style.fill);
- Mpaint.settextsize (35.0f);
- Canvas.drawtext ("Click Me Refresh", ten, mpaint);
- }
- public void ChangeColor () { //In order to let the outside call
- color++;
- }
- }
2. Layout
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <xue.test.CusView3
- android:id="@+id/cusview3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- >
- </xue.test.CusView3>
- </linearlayout>
3. After drawing, set the Click event to the control, upload the parameters to the control, then invalidate refresh
[Java]View Plaincopy
- Public class Testcustomviewactivity extends Activity {
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- VIEW3 = (CUSVIEW3) Findviewbyid (R.ID.CUSVIEW3);
- //Click events
- View3.setonclicklistener (new View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- Message message = new Message ();
- Message.what = 1;
- Myhandler.sendmessage (message);
- }
- });
- }
- Handler MyHandler = new Handler () {
- //Receive message after processing
- public void Handlemessage (Message msg) {
- switch (msg.what) {
- case 1:
- //Call method
- View3.changecolor ();
- //Refresh method
- View3.invalidate ();
- Break ;
- }
- super.handlemessage (msg);
- }
- };
- private CUSVIEW3 view3;
- }
As for the problem with custom controls taking up the entire screen, you may need to use Layoutparams
SOURCE Download: http://download.csdn.net/detail/ethan_xue/4152203
Android custom Controls (vi) Refresh