android - ImageView with no source does not honor layout, adding source does not repair layout -
my base layout listview of linearlayouts, , each item in grid framelayout below. downloading image imageview. prior image being loaded, there no content , layout shrunk height of small progressbar (first problem), when expect 240dp or 120dp. after image placed in view, layout not adjust (second problem) , height remains shrunken dimension of small progressbar.
loading image code:
@override public view getview(view convertview) { // ... // set holder // ... new getphototask().execute(holder.my_tile_image, holder.my_loading, my_media_url); holder.my_tile_image.setvisibility(view.invisible); holder.my_loading.setvisibility(view.visible); holder.my_tile_label.settext(activityobject1.track.name); // ... } private final class getphototask extends asynctask<object, void, drawable> { imageview iv; progressbar pb; @override protected drawable doinbackground(object... params) { iv = (imageview) params[0]; pb = (progressbar) params[1]; return new bitmapdrawable(getresources(), loadasset(params[2]).media_url, true)); } @override protected void onpostexecute(drawable result) { super.onpostexecute(result); pb.setvisibility(view.gone); iv.setimagedrawable(result); iv.setvisibility(view.visible); } }
the main xml:
<listview android:id="@+id/my_list" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="2dp" />
the xml each row:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_row_type_3" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="240dp" android:weightsum="3" > <include layout="@layout/my_tile_layout" android:id="@+id/my_tile_1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" /> <linearlayout android:id="@+id/my_row_type_2" android:orientation="vertical" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:weightsum="2" > <include layout="@layout/my_tile_layout" android:id="@+id/my_tile_2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <include layout="@layout/my_tile_layout" android:id="@+id/my_tile_3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </linearlayout> </linearlayout>
the xml row content:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_tile_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="2dp" > <imageview android:id="@+id/my_image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaletype="centercrop" android:contentdescription="@string/foo" /> <progressbar android:id="@+id/my_loading" style="?android:attr/progressbarstylesmall" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" /> <textview android:id="@+id/my_tile_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/my_title" android:layout_gravity="left|bottom" /> </framelayout>
why initial layout shrink height of progress bar though i've hard coded 240dp each row? why setting bitmap in imageview content not cause resize @ point either? first question important, believe nullify second. in advance.
solution
to structure want, use relativelayout tiles (for layering ability), , each row overriding linearlayout enforce sizing want set each row. there potential each tile's content larger or smaller view, screwing ability rely on either centercrop or fitcenter. in custom layout, set height proportional width of each row, , mode of measurespec exactly.
public final class myrow3 extends linearlayout { // ... @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, makemeasurespec(getsize(widthmeasurespec) * 2 / 3, measurespec.exactly)); }
i'm not sure why isn't honouring why not instead:
set absolute height in imageview guarantee space.
<imageview android:layout_height="###dp" />
then change layout auto adjusted state before image loaded:
iv.setlayoutparams( new linearlayout.layoutparams( linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent ) );