After talking about this, what is the size of the View? In this section, I will study the size of the View. Use LogCat to study how the View Size is determined. Okay. Let's get started with the subject.
1. The size of the View when new HelloView is directly added to the Activity.
You can use either of the following methods to obtain the View Size:
This. GetHeight (): Get the height of the View
This. GetWidth (): Get the View width
We can make a conjecture: When is the View size determined? Is it in the new View or in the onDraw? Or at other times? To study this, we patch the Log in the constructor and onDraw respectively (this is just what I used to call ).
---> HelloVew. java
PublicHelloView (Context context ){ Super(Context ); Log.V("HelloView (Context context)", "" +This. GetHeight () + "" +This. GetWidth ()); } /** * This is what we need to initialize in XML **/ PublicHelloView (Context context, AttributeSet attrs ){ Super(Context, attrs ); Log.V("HelloView (Context context, AttributeSet attrs)", "" +This. GetHeight () + "" +This. GetWidth ()); } /** * Draw a View **/ Protected VoidOnDraw (Canvas canvas ){ Log.V("OnDraw (Canvas canvas)", "" +This. GetHeight () + "" +This. GetWidth ()); Canvas. drawColor (Color.WHITE); MyUseBitmapFactory (canvas ); MyUseBitmapDrawable (canvas ); MyUseInputStreamandBitmapDrawable (canvas ); } |
Run:
We can see that the size of the View is not determined during the new View, and the system does not call the constructor (Context context.
That is to say, the size of the View is determined before OnDraw after the new View, and there are other methods before onDraw. Haha, let's try the override method:
Protected VoidOnMeasure (IntWidthMeasureSpec,IntHeightMeasureSpec ){ //TODOAuto-generated method stub Super. OnMeasure (widthMeasureSpec, heightMeasureSpec ); Log.V("OnMeasure", "" +This. GetHeight () + "" +This. GetWidth ()); } |
Run:
We observed that the onMeasure method was run twice: the width and height of the first operation were both 0, but the second operation changed. Is it determined in this method, but in fact, this is not necessarily the case. We will study it later. Here we only need to know that it is not determined in the new View.
Ii. View Size defined in XML
The Code is as follows:
Modify the main. xml file:
<? 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" > <TextView Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/hello" /> <View class = "com. fxhy. stady. HelloView" Android: layout_width = "50dip" Android: layout_height = "120dip" /> </LinearLayout> |
MainActivity:
/** * Use a custom View **/ Public ClassMainActivityExtendsActivity { Public VoidOnCreate (Bundle savedInstanceState ){ Super. OnCreate (savedInstanceState ); SetContentView (R. layout.Main); // Use a custom View } } |
Run: