[英]Add text inside Button drawable in Android
I've got Android project in Xamarin. 我在Xamarin有Android项目。 What I need to do is to add text inside image (green dot) placed in Button. 我需要做的是在Button中放置图像(绿点)内部添加文本。
As you see I am adding green dot as drawable. 如你所见,我正在添加绿点作为drawable。 So maybe I should make custom drawable which will be combined TextView and Image? 那么也许我应该制作自定义drawable,将TextView和Image结合起来?
This is how it looks now / what I am trying to achieve: 这就是它现在看起来/我想要实现的目标:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button_synchronize"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_action"
android:minHeight="60dp"
android:maxHeight="60dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:paddingLeft="20dp"
android:paddingRight="60dp"
android:layout_alignParentBottom="true"
android:text="Synkronisering"
android:drawableRight="@drawable/ic_green_dot"
android:drawablePadding="3dp"
android:gravity="center" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape>
<solid android:color="@color/main_orange_color" />
<stroke android:width="1dp" android:color="@color/main_grey_color" />
<corners android:radius="4dp" />
<padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
You can start with a FrameLayout
with width and height both set to wrap_content
. 您可以从FrameLayout
开始,其宽度和高度都设置为wrap_content
。 Then, add the Button
as the view in the bottom. 然后,将Button
添加为底部的视图。 Add the TextView
with appropriate styles applied. 添加应用了适当样式的TextView
。 Change its layout_gravity
appropriately. 适当地改变它的layout_gravity
。
For more, please have a look at: http://blog.neteril.org/blog/2013/10/10/framelayout-your-best-ui-friend/ 有关更多信息,请查看: http : //blog.neteril.org/blog/2013/10/10/framelayout-your-best-ui-friend/
The secret of FrameLayout is how it layouts its children. FrameLayout的秘诀在于它如何布置其子项。 Although normally designed to contain one item, it will happily stack up other element on top of each other. 虽然通常设计为包含一个项目,但它会很乐意将其他元素叠加在一起。 Thus FrameLayout is essentially a way to manipulate the Z-order of views on the screen. 因此,FrameLayout本质上是一种操纵屏幕上视图的Z顺序的方法。
You don't have to use Button
. 您不必使用Button
。 You can use any of RelativeLayout
, LinearLayout
, FrameLayout
etc. and their combinations. 您可以使用RelativeLayout
, LinearLayout
, FrameLayout
等中的任何一个及其组合。 When you assign an OnClickListener
to them, they will act as a button already. 为它们分配OnClickListener
时,它们将充当按钮。 So, let you Button
be a Layout
. 所以,让Button
成为一个Layout
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.