【问题标题】:How to add Image in Editbox with some text?如何在编辑框中添加带有一些文本的图像?
【发布时间】:2014-03-13 12:18:33
【问题描述】:

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

这是我的 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 ");

    }
 }

【问题讨论】:

标签: java android editbox


【解决方案1】:

你也可以试试这个方法

<EditText

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

或者您可以StackOverFlow Answer作为参考

所以你可以使用android:drawableRight=""

【讨论】:

  • 我做到了,但它没有给我图像,那是我的问题。请再看问题 1 次。
  • @AmitJayaswal 发布输出图像和所需图像
【解决方案2】:

您想要的功能称为CompoundDrawable

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

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

【讨论】:

    【解决方案3】:

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

        favButton.setCompoundDrawablesWithIntrinsicBounds( 0, 0,R.drawable.white_star, 0);
    

    【讨论】:

    • 这就是我在回答中所说的,但您是以编程方式完成的。
    【解决方案4】:

    如果您想继续使用 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 图像。 您也可以采用相对布局来执行此操作...更多继续谷歌搜索...

    【讨论】:

    • 是的,我使用的是 png 图片。
    • 你有答案吗?
    • 是的,我得到了答案,我也发布了@Banku ...:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多