开发者

custom imageview in larger layout

开发者 https://www.devze.com 2023-02-25 08:48 出处:网络
Having a bit of trouble here. I\'ve created a custom ImageView by subclassing ImageView and I\'m able to get it to display if I simply instantiate my view and set the entire contentView of my activity

Having a bit of trouble here. I've created a custom ImageView by subclassing ImageView and I'm able to get it to display if I simply instantiate my view and set the entire contentView of my activity to my imageview vi开发者_Go百科a setContentView(myCustomImageView)

But what if I want to just use my custom imageview as part of a larger layout? E.g., I want a button, a textview, etc and then my ImageView somewhere in there...

I tried doing something like programatically creating a linearlayout and then instantiating my special imageview and adding it via mLinearLayout.addView(myCustomImageView) but it crashes when I do this.

Am I missing something basic here?

EDIT: apparently you just use the full namespace of your custom view in the XML.


Why don't you put your layout in the XML?

You can add you custom View like this:

<view
    class="com.your_package.myCustomImageView" 
    id="@+id/customView"
/>

You can find more useful resources on the Android SDK documentation site

Edit: Yes, you must use the entire namespace for your self defined views. For standard views you can omit com.android.widget because the SDK knows where to find them, but for your own stuff he doesn't.

0

精彩评论

暂无评论...
验证码 换一张
取 消