简体   繁体   English

如何在Android应用程序中的每个ListView项目文本之前添加图标?

[英]How to add an icon before every ListView item text in an Android Application?

How can I add an icon before every ListView item text in an Android application? 如何在Android应用程序中的每个ListView项目文本之前添加图标?

Here is the current list_item xml that is called to populate the list: 这是被调用以填充列表的当前list_item xml:

<?xml version="1.0" encoding="utf-8"?>


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:gravity="center_vertical"
    android:padding="5dip"
    android:background="@color/darkblue"
/>   

I attempted to add a ImageView before it, but it didn't show at all. 我试图在其之前添加一个ImageView,但它根本没有显示。 Then I added a RelativeLayout around the entire thing and tried positioning them, but still it didn't show. 然后,我在整个对象周围添加了一个RelativeLayout,并尝试放置它们,但是仍然没有显示。

Any ideas? 有任何想法吗?

Thanks! 谢谢!

You can use a Relative Layout, just add the property android:layout_toRightOf to your TextView:: 您可以使用相对布局,只需将android:layout_toRightOf属性添加到TextView :::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal">
<ImageView
        android:id="@+id/myIcon"
        android:layout_width="25px"
        android:layout_height="25px"       
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:padding="4px"
        />  
<TextView
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:gravity="center_vertical"
    android:padding="5dip"
    android:background="@color/darkblue"
android:layout_toRightOf="@id/myIcon"  
/>   

</RelativeLayout>

Better way to do this is to use drawableLeft , drawableRight , drawableTop , drawableBottom tags: 更好的方法是使用drawableLeftdrawableRightdrawableTopdrawableBottom标签:

<?xml version="1.0" encoding="utf-8"?>


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:gravity="center_vertical"
    android:padding="5dip"
    android:background="@color/darkblue"
    android:drawableLeft="@drawable/yourPicture"

/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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