Better to inflate or create controls in Android?

I am wondering if anyone can shed some insights on best practices for dynamically creating controls (inflate vs instance).

Pump up:

TextView styledText = (TextView)inflater.inflate(R.layout.styledTextView);

Instantiate:

TextView styledText = new TextView(mContext);
styledText.setTextAppearance(R.style.StyledTextStyle);

The created object can either contain attributes in an inflated XML file, or it can be contained in a style definition, which is subsequently added to the object instance. (Suppose this style includes width, background, text color, etc.).

Failed to perform any time / memory tests of each method, I wondered if anyone knew what would be the fastest / most efficient.

+5
source share
2 answers

LayoutInflator , xml . . , View , . -, , - . 99,9% , .

, , xml, "setTextAppearance", XML. - XML- TextView, .

+10

, , , , , /, new (aka instantiate), , ImageButton, XML , .

inflate, ImageButton .

, :

:)

    LayoutInflater inflater = LayoutInflater.from(getActivity());
    CardView myCardView = (CardView) inflater.inflate(R.layout.my_cardview, null);
    ImageView icon = (ImageView) myCardView.findViewById(R.id.iconId);

~~~~~~~~~~~~

:(.. null

CardView myCardView = CardView (getActivity());

ImageView icon = (ImageView) myCardView.findViewById(R.id.iconId);

0

All Articles