Android: combining text & image on a Button or ImageButton
我正在尝试在按钮上创建一个图像(作为背景)并动态添加,具体取决于运行时发生的情况,图像上方/上方的一些文本。
如果我使用
如果我使用
然而,这些属性仅在x和y维度中组合文本和图像,这意味着我可以在文本周围绘制图像,但不能在文本的下方/下方(z轴定义为从显示中出来)。
有关如何做到这一点的任何建议? 一种想法是扩展
对于只想将背景,图标图像和文本放在不同文件中的一个
1 2 3 4 5 6 7 8 9 10 | <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/home_btn_test" android:drawableTop="@drawable/home_icon_test" android:textColor="#FFFFFF" android:id="@+id/ButtonTest" android:paddingTop="32sp" android:drawablePadding="-15sp" android:text="this is text"></Button> |
对于更复杂的排列,您还可以使用
教程:涵盖两种情况的精彩教程:http://izvornikod.com/Blog/tabid/82/EntryId/8/Creating-Android-button-with-image-and-text-using-relative-layout.aspx
这个问题有一个更好的解决方案。
只需取正常的
1 2 3 4 5 | <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/my_btn_icon" android:gravity="left|center_vertical" /> |
通过这种方式,您可以获得一个按钮,该按钮在按钮的左侧显示一个图标,并且图标的右侧站点的文本垂直居中。
您可以在
任何文本都将显示在背景上方。
如果你在xml中寻找类似的东西,有:
1 2 3 4 5 6 7 8 9 10 11 12 | <Button android:layout_width="0dp" android:layout_weight="1" android:background="@drawable/home_button" android:drawableLeft="@android:drawable/ic_menu_edit" android:drawablePadding="6dp" android:gravity="left|center" android:height="60dp" android:padding="6dp" android:text="AndroidDhina" android:textColor="#000" android:textStyle="bold" /> |
只需使用LinearLayout并假装它是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <LinearLayout android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:drawable/btn_default" android:clickable="true" android:orientation="horizontal"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:src="@drawable/image" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_margin="5dp" android:text="Do stuff" /> </LinearLayout> |
只是替换
1 | android:background="@drawable/icon" |
同
1 2 | android:background="@android:color/transparent" android:drawableTop="@drawable/[your background image here]" |
izz是一个很好的技巧..;)
我从这里提到的方法采取了不同的方法,它工作得非常好,所以我想分享它。
我正在使用Style创建一个自定义按钮,左侧是图像,右侧是文本。只需按照下面的4个"简单步骤":
I.使用至少3个不同的PNG文件和您拥有的工具创建9个补丁:/ YOUR_OWN_PATH/android-sdk-mac_x86/tools/./draw9patch。在此之后你应该:
button_normal.9.png,button_focused.9.png和button_pressed.9.png
然后下载或创建一个24x24 PNG图标。
ic_your_icon.png
将所有内容保存在Android项目的drawable /文件夹中。
II。在drawable /文件夹下的项目中创建名为button_selector.xml的XML文件。州应该是这样的:
1 2 3 | <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <item android:drawable="@drawable/button_normal" /> |
III。转到values /文件夹并打开或创建styles.xml文件并创建以下XML代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <style name="ButtonNormalText" parent="@android:style/Widget.Button"> <item name="android:textColor">@color/black</item> <item name="android:textSize">12dip</item> <item name="android:textStyle">bold</item> <item name="android:height">44dip</item> <item name="android:background">@drawable/button_selector</item> <item name="android:focusable">true</item> <item name="android:clickable">true</item> </style> <style name="ButtonNormalTextWithIcon" parent="ButtonNormalText"> <item name="android:drawableLeft">@drawable/ic_your_icon</item> </style> |
ButtonNormalTextWithIcon是一个"子样式",因为它正在扩展ButtonNormalText("父样式")。
请注意,将ButtonNormalTextWithIcon样式中的drawableLeft更改为drawableRight,drawableTop或drawableBottom,可以将图标放置在相对于文本的其他位置。
IV。转到您拥有用于UI的XML的布局/文件夹,然后转到要应用样式的Button,并使其如下所示:
1 2 3 4 5 | <Button android:id="@+id/buttonSubmit" android:text="@string/button_submit" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/ButtonNormalTextWithIcon"></Button> |
而且......瞧!你的按钮左侧有一个图像。
对我来说,这是更好的方法!因为这样做,您可以单独管理要显示的图标的按钮文本大小,并使用相同的背景可绘制几个按钮,使用不同的图标,使用样式的Android UI准则。
您还可以为应用程序创建主题并为其添加"父样式",以便所有按钮看起来都相同,并将"子样式"仅应用于您需要的图标。
1 2 3 4 5 6 7 | <Button android:id="@+id/imeageTextBtn" android:layout_width="240dip" android:layout_height="wrap_content" android:text="Side Icon With Text Button" android:textSize="20sp" android:drawableLeft="@drawable/left_side_icon" /> |
您可以使用
1 2 3 4 5 6 7 8 9 10 11 12 | <Button android:id="@+id/btn_video" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@null" android:drawableTop="@drawable/videos" android:gravity="left|center_vertical" android:onClick="onClickFragment" android:text="Videos" android:textColor="@color/white" /> |
重要更新
Don't use normal
android:drawableLeft etc... with vector drawables, else it
will crash in lower API versions. (I have faced it in live app)
对于矢量drawable
如果你使用矢量drawable,那么你必须
- 你有没有迁移到AndroidX?如果不是,您必须先迁移到AndroidX。这很简单,看看什么是androidx,以及如何迁移?
-
它是在版本
1.1.0-alpha01 中发布的,因此appcompat版本至少应为1.1.0-alpha01 。当前的最新版本是1.1.0-alpha02 ,使用最新版本以获得更好的可靠性,请参阅发行说明 - 链接。implementation 'androidx.appcompat:appcompat:1.1.0-alpha02' -
使用
AppCompatTextView /AppCompatButton /AppCompatEditText -
使用
app:drawableLeftCompat ,app:drawableTopCompat ,app:drawableRightCompat ,app:drawableBottomCompat ,app:drawableStartCompat 和app:drawableEndCompat
适用于常规抽奖
如果你不需要矢量绘图,那么你可以
-
使用
android:drawableLeft ,android:drawableRight ,android:drawableBottom ,android:drawableTop -
您可以使用常规
TextView ,Button 和EditText 或AppCompat 类。
您可以实现如下输出 -
可能我的解决方案适合很多用户,我希望如此。
我建议你用你的风格制作TextView。它非常适合我,并且具有所有功能,如按钮。
首先让make按钮样式,你可以在任何地方使用...我正在创建button_with_hover.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="rectangle" > <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#8dbab3" /> <gradient android:angle="-90" android:startColor="#48608F" android:endColor="#48608F" /> </shape> <!--#284682;--> <!--border-color: #223b6f;--> </item> <item android:state_focused="true"> <shape android:shape="rectangle" > <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#284682" /> <solid android:color="#284682"/> </shape> </item> <item > <shape android:shape="rectangle" > <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="@color/ControlColors" /> <gradient android:angle="-90" android:startColor="@color/ControlColors" android:endColor="@color/ControlColors" /> </shape> </item> </selector> |
其次,
让我们创建一个textview按钮。
1 2 3 4 5 6 7 8 9 10 11 12 | <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dip" android:layout_gravity="right|bottom" android:gravity="center" android:padding="12dip" android:background="@drawable/button_with_hover" android:clickable="true" android:drawableLeft="@android:drawable/btn_star_big_off" android:textColor="#ffffffff" android:text="Golden Gate" /> |
这就是结果。然后使用任何颜色或任何其他属性和边距设置自定义按钮的样式。祝好运
1 2 3 4 5 6 7 8 9 10 11 12 | <Button android:id="@+id/groups_button_bg" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Groups" android:drawableTop="@drawable/[image]" /> android:drawableLeft android:drawableRight android:drawableBottom android:drawableTop |
http://www.mokasocial.com/2010/04/create-a-button-with-an-image-and-text-android/
这段代码非常适合我
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <LinearLayout android:id="@+id/choosePhotosView" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" android:clickable="true" android:background="@drawable/transparent_button_bg_rev_selector"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/choose_photo"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:text="@string/choose_photos_tv"/> </LinearLayout> |
你可以用这个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <Button android:id="@+id/reset_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/btn_med" android:text="Reset all" android:textColor="#ffffff" /> <Button android:id="@+id/undo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@drawable/btn_med" android:text="Undo" android:textColor="#ffffff" /> |
因为我把图像作为
1 2 3 4 5 6 7 | <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/temp" /> |