/ / Ako dynamicky nafúknuť rozloženie? - Java, Android, Eclipse, Android-layout, layout-inflater

Ako dynamicky nafúknuť rozloženie? - java, android, eclipse, android-layout, layout-inflater

V XML mám definované nasledujúce rozloženie:

<?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>

Ako môžem použiť LayoutInflater na uchopenie ListView a ProgressBar a jeho priradenie v kóde?

odpovede:

21 pre odpoveď č. 1

Touto cestou:

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

7 pre odpoveď č. 2

Potrebujete iba referenciu na aktivitu a zavolajte:

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

Potom môžete tieto dva pohľady chytiť pomocou ich id

relativeLayout.findViewById(R.id.anId);

0 pre odpoveď č. 3

skúste to

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 pre odpoveď č. 4
private LayoutInflater mInflater;
View convertView;
mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.xml.yourxmlname, null);

teraz máte prístup k položkám podľa

convertView.findViewById

alebo ak máte veľa inštancií, môžete definovať diváka

static class CalViewHolder {
ListView con_list;
Progressbar con_progress;
}
holder = new CalViewHolder();
convertView.setTag(holder);