开发者

keeping references to inflated custom views

开发者 https://www.devze.com 2022-12-28 07:36 出处:网络
While researching how to create custom compound views in Android, I have come across this pattern a lot (example comes from the Orange11 blog) :

While researching how to create custom compound views in Android, I have come across this pattern a lot (example comes from the Orange11 blog) :

public class FirstTab extends LinearLayout {
private ImageView imageView;
private TextView textView;
private TextView anotherTextView;

public FirstTab(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.firstTab, this);
}

}

I mostly understand how this is working, except for the part where inflate() is called. The documentation says that this method returns a View object, but in this example the author does not store the re开发者_如何转开发sult anywhere. After inflation, how is the new View created fromt eh XML associated with this class? I thought about assigning it to "this", but that seems very wrong.

thanks for any clarification.


The reference to this would be the viewgroup root. See here: http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int, android.view.ViewGroup)

What this means is it is inflating the designated view from xml with this as the parent view. The xml ends up inside the Linear layout defined by the class.

edit: put in the full link as I can't seem to get URLs with brackets to escape properly

0

精彩评论

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

关注公众号