android – 里面有文字的图标 – 如何在布局中定义?

android – 里面有文字的图标 – 如何在布局中定义?,第1张

概述我希望将一个TextView(显示计数)悬停在ActionBar中的一个图标上.我所做的是有一个RelativeLayout,为它设置一个背景,然后将TextView放在该布局中并拧上边距直到它适合,但是一旦文本长度变化就会被打破. 在TextView上设置背景也不是很好,因为我无法将文本放置在与图标相关的位置. 这是我的XML: <?xml version="1.0" encoding="utf 我希望将一个TextVIEw(显示计数)悬停在Actionbar中的一个图标上.我所做的是有一个relativeLayout,为它设置一个背景,然后将TextVIEw放在该布局中并拧上边距直到它适合,但是一旦文本长度变化就会被打破.
在TextVIEw上设置背景也不是很好,因为我无法将文本放置在与图标相关的位置.

这是我的XML:

<?xml version="1.0" enCoding="utf-8"?> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical"    androID:layout_gravity="fill_horizontal" >    <relativeLayout        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignParentRight="true"        androID:layout_centerVertical="true"        androID:background="@drawable/cart" >    <TextVIEw        androID:ID="@+ID/textVIEw1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="0"        androID:layout_margintop="5dp"      androID:layout_marginleft="43dp"        androID:textAppearance="?androID:attr/textAppearanceMedium" /></relativeLayout> </relativeLayout>

这是一个屏幕截图,向您展示我的意思:

是否有一种无压力的方式,也许是一个库,它允许我在Drawable上设置TextVIEw,以便TextVIEw始终居中/定位?

解决方法

Setting a background on the TextVIEw isn’t great either because I can’t position the text in relation to the icon.

实际上,您可以相对于图标定位文本.您需要将购物车图标和数字徽章图标分开为单独的图像,然后单独列出.不久前我不得不这样做,我用relativeLayout做了,带有购物车图标的ImageVIEw和带有9个贴片“徽章”作为背景的数字的TextVIEw.

诀窍是将数字TextVIEw与购物车图标ImageVIEw的左下角对齐,然后使用左下边距将数字徽章推到购物车图标的顶部和右侧.这样,数字徽章始终根据您的购物车图标进行锚定.

此外,将TextVIEw的重力设置为居中,以便随着数字变宽,文本的相对位置大致相同.最后,在TextVIEw上使用填充来控制数字边缘与“徽章”9补丁边缘之间的间隙.

这是我的实现的片段(我已经修改了一些):

<relativeLayout        androID:ID="@+ID/cartbutton"        androID:layout_wIDth="wrap_content"        androID:layout_height="fill_parent" >        <ImageVIEw            androID:ID="@+ID/cartIconImageVIEw"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_centerHorizontal="true"            androID:src="@drawable/ic_menu_cart" />        <TextVIEw            androID:ID="@+ID/cartBadge"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_alignBottom="@+ID/cartIconImageVIEw"            androID:layout_alignleft="@+ID/cartIconImageVIEw"            androID:layout_marginBottom="10dp"            androID:layout_marginleft="9dp"            androID:background="@drawable/state_List_cart_badge"            androID:gravity="center"            androID:includeFontpadding="false"            androID:lines="1"            androID:paddingleft="8dp"            androID:paddingRight="8dp"            androID:text="7" />    </relativeLayout>

这就是它的样子:

总结

以上是内存溢出为你收集整理的android – 里面有文字的图标 – 如何在布局中定义?全部内容,希望文章能够帮你解决android – 里面有文字的图标 – 如何在布局中定义?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1127369.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存