开发者

Android Add imagine icon as a "hint" in a textview?

开发者 https://www.devze.com 2023-03-18 19:18 出处:网络
i开发者_运维技巧s it possible to add a image icon inside a textviews Hint? at the moment i can only add text in the hint properties but what i want to do is add a small image icon and a text.

i开发者_运维技巧s it possible to add a image icon inside a textviews Hint?

at the moment i can only add text in the hint properties but what i want to do is add a small image icon and a text.

is this possible?


You will need to write your own View which could perhaps be based on a FrameLayout which contains both a TextView (with an empty hint) and an ImageView. Alternatively you could simply use a TextView and when it's empty you set a background image to your "hint icon", and clear the background when text has been entered.


You could user setError to display some text and and an image in the textfield but that would be after the user inputs something and you check it.

Alternatives:

  1. Use setBackground with your hint icon on the background image
  2. Subclass TextView and override the drawing to draw your hint icon


    String hint = "<center><img src=\"" + R.drawable.search_hint + "\"/>搜索感兴趣的内容, hello world , I'm form China.</center>";

    Html.fromHtml(hint, new ImageGetter()
    {

        @Override
        public Drawable getDrawable(String source)
        {
            int img = Integer.parseInt(source);
            Drawable drawable = getResources().getDrawable(img);
            drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

            return drawable;
        }
    }, null);
    searchTxt.setHint(hint);
0

精彩评论

暂无评论...
验证码 换一张
取 消