How to inflate a layout dynamically?

后端 未结 4 1422
时光取名叫无心
时光取名叫无心 2020-12-18 04:10

I have the following layout defined in XML:


    

        
相关标签:
4条回答
  • 2020-12-18 04:20

    You just need the activity reference and call :

    RelativeLayout relativeLayout = (RelativeLayout)activity.getLayoutInflater().inflate(R.layout.your_layout, null);
    

    Then you can grab the two view using their ids

    relativeLayout.findViewById(R.id.anId);
    
    0 讨论(0)
  • 2020-12-18 04:37

    In this way:

    View v = getLayoutInflater().inflate(R.layout.YOUR_LAYOUT_ID, null);
    ListView listview = (ListView) v.findViewById(R.id.streamListView);
    ProgressBar progress = (ProgressBar) v.findViewById(R.id.streamProgressBar);
    
    0 讨论(0)
  • 2020-12-18 04:41
    private LayoutInflater mInflater;
    View convertView;
    mInflater = LayoutInflater.from(context);
    convertView = mInflater.inflate(R.xml.yourxmlname, null);
    

    now you can access the items by

    convertView.findViewById
    

    or if you hav lots of Instances, you may define a viewholder

    static class CalViewHolder {
        ListView con_list;
        Progressbar con_progress;
    }
    holder = new CalViewHolder();
    convertView.setTag(holder);
    
    0 讨论(0)
  • 2020-12-18 04:42

    try this

    RelativeLayout myLayout = (RelativeLayout)getLayoutInflater().inflate(
                    R.layout.you_xml_id, null);
    
            ListView list = (ListView)myLayout.findViewById(R.id.streamListView);
            ProgressBar bar = (ProgressBar)myLayout.findViewById(R.id.streamProgressBar);
    
    0 讨论(0)
提交回复
热议问题