繁体   English   中英

将图像与按钮内的文本组合

[英]Combining an Image with Text inside a Button

大家好,我是编码新手,目前正在学习 xml 和 java for android。 我试图制作一个显示 GOAL 的按钮,但它不是“O”,而是一个球的图像,我试着在一个设计器程序中制作一个图像,上面写着目标,里面有球的图标,然后使用它作为按钮的图像背景,但它错过了灰色(我打算将其更改为绿色)背景,这并不是我真正想要的。 有没有办法只在具有文本的按钮内添加图像? 或者我应该在不同的软件中设计按钮,而不是将其添加为按钮的背景?

像“Te~ImageHere~xt”

您可以使用ImageSpan在文本之间添加drawable 下面是一个例子。

  TextView textView=(TextView)findViewById(R.id.b1);
    Spannable span = new SpannableString("G OL");
    Drawable android = getResources().getDrawable(R.mipmap.ic_launcher);
    android.setBounds(0, 0, 50,50);
    ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE);
    span.setSpan(image, 1, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    textView.setText(span);

下面是输出。 您可以修改边界。 在此处输入图片说明

试试这个使用android:drawableLeft="@mipmap/ic_launcher_round"Button添加图像

<Button
    android:text="Prem"
    android:drawableLeft="@mipmap/ic_launcher_round"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

您还可以像下面的代码一样使用drawableTopdrawableBottomdrawableRightdrawableEnd

android:drawableTop="@mipmap/ic_launcher_round"
android:drawableBottom="@mipmap/ic_launcher_round"
android:drawableRight="@mipmap/ic_launcher_round"
android:drawableLeft="@mipmap/ic_launcher_round"
android:drawableEnd="@mipmap/ic_launcher_round"

暂无
暂无

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

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