0

我正在尝试在编辑框中添加带有一些文本的图像,但它没有显示图像。它只显示文本。

这是我的xml。

 <Button
     android:id="@+id/favButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Add to "
     android:drawableRight="@drawable/white_star"
     android:layout_below="@id/priceText"
     android:layout_alignParentLeft="true"
     android:background="@drawable/rounded_corner_black"
     android:padding="10dip"
     style="@style/ButtonText"
     android:onClick="onAddFavClick" />

这是我的 Java 代码。

 private void updateFavButton() {
    if (atmItem.isFav()) {

      favButton.setText("Remove from ");

    } else {

      favButton.setText("Add to ");

    }
 }
4

4 回答 4

0

你也可以试试这个方法

<EditText

    android:drawableLeft="@drawable/your_image" />

或者作为参考,您可以StackOverFlow 回答

因此,您可以使用android:drawableRight=""

于 2014-02-14T08:33:30.710 回答
0

您想要的功能称为CompoundDrawable.

您可以在其中设置一些属性EditText

在您的情况下,由于您想在右侧显示图像,请使用android:drawableRight.

于 2014-02-14T08:35:25.707 回答
0

如果您想继续使用 XML,这是一种简单的方法

<RelativeLayout 
    android:id="@+id/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/searchcards_back_relative" >

<ImageView 
                android:id="@+id/leftbutton"
                android:layout_alignParentLeft="true"
                android:layout_width="wrap_content"
                android:layout_height="46dp"
                android:background="@drawable/searchleft"/>    

<LinearLayout android:focusable="true"
    android:id="@+id/searchcards_edit_search_lin"
    android:focusableInTouchMode="true" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/leftbutton"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="0px"
    android:background="@drawable/searchmiddle">

    <EditText 
        android:hint="Search Cards" 
        android:id="@+id/searchcards_edit_search"
        android:layout_width="fill_parent"
        android:layout_height="28dp"
        android:textColor="#000000" 
        android:textSize="18dp"
        android:singleLine="true"
        android:background="#0fff"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="21dp"
        android:gravity="center_vertical"
        android:layout_marginTop="0dp"
        android:imeOptions="actionSearch"
        android:layout_gravity="center_vertical"/>

</LinearLayout>
<ImageView 
            android:id="@+id/rightbutton"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="46dp"
            android:background="@drawable/searchright"/> 

    <TextView  
        android:id="@+id/searchcards_back_hitsearch"
        android:layout_width="35dp" 
        android:layout_height="40dp"
        android:textSize="25px"
        android:textStyle="bold"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white"
        android:layout_alignParentRight="true"  
        android:background="#0222"    
        android:text=""/>

</RelativeLayout>

<ListView
    android:id="@+id/searchcards_list"
    android:layout_width="wrap_content"
    android:background="@drawable/background"
    android:layout_height="fill_parent"
    android:layout_below="@+id/edittext"
    android:divider="#fff"
    android:dividerHeight="1px"
    android:cacheColorHint="@android:color/transparent"
        android:layout_marginBottom="4px"
    android:scrollbars="none"   />

 </RelativeLayout>

您应该在可绘制文件夹中为左、中和右图像使用 .png 图像。你也可以采取相对布局来做到这一点......更多继续谷歌搜索......

于 2014-02-14T08:40:44.480 回答
0

我通过以下方法简单地得到了图像

    favButton.setCompoundDrawablesWithIntrinsicBounds( 0, 0,R.drawable.white_star, 0);
于 2014-02-14T08:42:53.317 回答