Android add a class
My TextSwitcher c开发者_高级运维lass extends view. How can I add it in to my main file and envoke methods in the class?
Ive tried:
addView(new TextSwitcher(this));
But no luck.
By "my main file", you mean the main layout file? First, you can do that straight in the layout XML. The syntax is:
<com.mypackage.TextSwitcher android:id="@+id/TheSwitcher" />
and don't forget width/height/weight/etc.
Second, you can add it programmatically via addView()
. Be careful to pick the right container object for that. And don't forget to set its layout params, which must be compatible with the container (for objects in LinearLayout, you provide params as as instance of LinearLayoutParams, etc.).
I recommend the first approach though. Layout params are not particularly code-friendly, especially if you want density-independent sizing.
精彩评论