7

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>

LayoutInflater を使用して ListView と ProgressBar を取得し、コードで割り当てるにはどうすればよいですか?

4

4 に答える 4

30

この上:

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);
于 2011-05-20T10:32:16.083 に答える
7

アクティビティ参照と呼び出しだけが必要です:

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

次に、IDを使用して2つのビューを取得できます

relativeLayout.findViewById(R.id.anId);
于 2011-05-20T10:29:04.920 に答える
1
private LayoutInflater mInflater;
View convertView;
mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.xml.yourxmlname, null);

アイテムにアクセスできるようになりました

convertView.findViewById

または、多数のインスタンスがある場合は、ビューホルダーを定義できます

static class CalViewHolder {
    ListView con_list;
    Progressbar con_progress;
}
holder = new CalViewHolder();
convertView.setTag(holder);
于 2011-05-20T10:32:43.570 に答える
0

これを試して

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);
于 2011-05-20T10:32:20.197 に答える