How to have a ListView with icons beside the text?

和自甴很熟 提交于 2019-12-12 02:49:08

问题


I'm trying to have a ListView with icon and text but there is a little problem as you can see in the screen shot (only a very small part of text is visible!):

Here is the xml files creating this layout:

//The overall layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <ListView
        android:id="@+id/ListView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

// this layout is for single rows
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
        <ImageView
        android:id="@+id/lIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        <TextView
        android:id="@+id/lText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    </LinearLayout>

Edit: This is the getView method of my custom adapter.

public View getView(int position, View convertView, ViewGroup parent) {
            View myView = convertView;                  
            cu.moveToPosition(position);
            if (convertView == null) {
                LayoutInflater li = getLayoutInflater();
                myView = li.inflate(R.layout.listicon, null);
               ImageView imageView = (ImageView) myView.findViewById(R.id.lIcon);
               myView.setLayoutParams(new GridView.LayoutParams(60, 90));
               myView.setPadding(8, 8, 8, 8);
               byte[] b = Base64.decode(cu.getString(2),Base64.DEFAULT);
               imageView.setImageBitmap(BitmapFactory.decodeByteArray(b, 0, b.length));
               TextView textView = (TextView) myView.findViewById(R.id.lText);
               textView.setText(cu.getString(1));
            }
            //cu.moveToNext();
            int catID = cu.getInt(0);
            myView.setTag((Object) catID);
            return myView;
        }

回答1:


Try setting layout_width of the ListView and TextView to fill_parent

EDIT: you're resetting list item width in the getView. Either remove or correct your code




回答2:


Use a relative layout for your rows and place the text right of the image as below :

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
    <ImageView
    android:id="@+id/lIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    <TextView
    android:id="@+id/lText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/lIcon"/>
</RelativeLayout>



回答3:


This is related to the way single row is inflated in the adapter

You should try using LayoutInflater#inflate(layoutId, parent, false) as suggested in this answer: https://stackoverflow.com/a/1662611




回答4:


Try this layout for the row layout: Adjust the IDs to yours.. also notice the android:layout_toRightOf id.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <ImageView
         android:id="@+id/bimage"
         android:layout_height="20dip"
         android:layout_width="20dip"
         android:layout_gravity="left|center"
         android:layout_marginTop="10dip"
         android:layout_marginLeft="5dip"
         android:layout_alignParentLeft="true"
         android:layout_centerVertical="true"
         />
    <TextView
        android:id="@+id/btitle"
        android:layout_toRightOf="@id/bimage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="8dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:textSize="18dip"
        android:textColor="#ffffff"
        android:gravity="center_vertical"
        />
</RelativeLayout>


来源:https://stackoverflow.com/questions/9452989/how-to-have-a-listview-with-icons-beside-the-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!