简体   繁体   中英

Android listview imageview and textview, if image empty so textview to left

On my code below would've been nice if the item has a picture. but if it is empty then the item picture looks empty box the size 70dp. I would like if the item does not contain an picture, then automatically the empty box filled with TextView, so if it is empty then the picture like no ImageView, only TextView. I tried to set the ImageView width size andoid:layout_width="wrap_content" it worked with what I want, but the size of the picture was disheveled. There are wide and small. therefore I set it so the 70 to make it look flat.

This is my code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/logo_article"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip" />

    <TextView
        android:id="@+id/title_article"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/logo_article"
        android:gravity="center_vertical"
        android:text="Title Article" />

</RelativeLayout>

I get items from database and I store to simple adapter, code in java:

String[] from = { "title", "logo" };
int[] to = { R.id.title_article, R.id.logo_article };
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),listkatdb,R.layout.per_item_list, from, to);
kategoriListView.setAdapter(adapter);
adapter.notifyDataSetChanged();

Thanks.

Don't fix your image view size. change image width as wrap_content . If image have means show test right of image. don't have image means image width 0dip text automatically comes left side. try this

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<ImageView
    android:id="@+id/logo_article"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip" />

<TextView
    android:id="@+id/title_article"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/logo_article"
    android:gravity="center_vertical"
    android:text="Title Article" />

    </RelativeLayout>

如果为空,则将ImageView的可见性设置为GONE

Use logo_article.setVisibility(View.GONE); if no content to show. thats it.

If there is no image, you need to set ImageView's setVisibility to GONE, then TextView will fill whole width.

if(isImageAvailable)
{
    iv.setVisibility(View.VISIBLE);
}
else
{
    iv.setVisibility(View.GONE);
}

Programmatically when you find out there is no image set visibility of ImageView to GONE

 ImageView img = (ImageView)findViewById(R.id.logo_article);
 // logic here to find image
 //if no img
 img.setVisibility(View.GONE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM