2011-05-20 3 views
3

私はXMLで定義された以下のレイアウトがあります。レイアウトを動的に拡張するには?

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/streamRelativeLayout"> 
     <ListView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/streamListView"></ListView> 
     <ProgressBar android:layout_centerInParent="true" android:layout_height="wrap_content" android:id="@+id/streamProgressBar" android:layout_width="wrap_content"></ProgressBar> 
    </RelativeLayout> 

は、どのように私はListViewコントロールとプログレスバーをつかむと、コードでそれを割り当てるためにLayoutInflaterを使用することができますか?

答えて

19

を使用して2つのビューをつかむことができます

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); 
7

は、あなただけの活動の参照が必要とコール:

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

viewholderを定義することにより、項目にアクセスすることができますll = new LinearLayout(コンテキスト);このレイアウトはframelayout、webviewのようないくつかのサブビューを持っています。今、この直線レイアウトをどのように膨らませることができますか? – Karthick

+1

レイアウトを拡張するときに、親ビューとしてnullを渡さないようにするため、ビュールートとしてnullを渡さないようにしてください(膨張したレイアウトのルート要素のレイアウトパラメータを解決する必要があります)。無視される。 –

0

これを試してみてください。

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

は、その後、あなたがそのIDこのように

relativeLayout.findViewById(R.id.anId); 
0
private LayoutInflater mInflater; 
View convertView; 
mInflater = LayoutInflater.from(context); 
convertView = mInflater.inflate(R.xml.yourxmlname, null); 

今、あなたは私がのLinearLayoutあるlinaerlayout動的に作成抱えてい

convertView.findViewById 

か、インスタンスをたくさん肝炎場合、あなたは

static class CalViewHolder { 
    ListView con_list; 
    Progressbar con_progress; 
} 
holder = new CalViewHolder(); 
convertView.setTag(holder); 
関連する問題