开发者

How to add layout from xml to Extended FrameLayout Android?

开发者 https://www.devze.com 2023-01-07 05:04 出处:网络
I have a custom class called NoteView that extends FrameLayout.Previously, I had just been using the stock LinearLayout and have an XML layout built already that I want to reuse by adding that entire

I have a custom class called NoteView that extends FrameLayout. Previously, I had just been using the stock LinearLayout and have an XML layout built already that I want to reuse by adding that entire hierarchy (with some other overlay views which is why I needed framelayout) to th开发者_JS百科e NoteView.

The problem is I can't figure out how to inflate the XML and add it to the NoteView from within the NoteView Class. I can add it after initialization is complete, but I want to be able to be able to inflate that hierarchy and add it automatically when my NoteView is either instantiated or inflated from XML.

How can I populate my extended FrameLayout with a layout from XML by adding it from within the NoteView class itself?


Try LayoutInflater

LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myView = (LinearLayout) inflater.inflate(R.layout.my_layout, null);
frameLayout.addChild(myView);

Have a look at the documentation for LayoutInflater here: http://d.android.com/reference/android/view/LayoutInflater.html

0

精彩评论

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