如何使用 XML 样式在 Android 中创建自定义按钮 [英] How to create custom button in Android using XML Styles

查看:50
本文介绍了如何使用 XML 样式在 Android 中创建自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这种按钮[相同的背景&text] 使用 XML 样式的颜色

I want to make this kind of button [same background & text] colors by using XML Styles

这只是一个例子,我想写一些其他的文字,比如:关于我

that's just for an example, i want to write some other texts, like: About Me

我仍然使用设计师在 Photoshop 中创建的按钮

Still i am using button created by designer in Photoshop

    <ImageButton
        android:id="@+id/imageButton5"
        android:contentDescription="AboutUs"
        android:layout_width="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view_pager"
        android:layout_centerHorizontal="true"
        android:background="@drawable/aboutus" />

注意:我需要各种尺寸和形状的按钮

Note: I need this kind of button in every size and shape

我不想在我的 Android 应用程序中使用任何图像,我只想使用 XML 来制作它

推荐答案

您是否尝试过为任何按钮创建背景形状?

Have you ever tried to create the background shape for any buttons?

在下面查看:

下面是从按钮图像中分离出来的图像.

Below is the separated image from your image of a button.

现在,把它放在你的 ImageButton for android:src "source" 中,如下所示:

Now, put that in your ImageButton for android:src "source" like so:

android:src="@drawable/twitter"

现在,只需创建 ImageButton 的形状以获得黑色着色器背景.

Now, just create shape of the ImageButton to have a black shader background.

android:background="@drawable/button_shape"

而 button_shape 是 drawable 资源中的 xml 文件:

and the button_shape is the xml file in drawable resource:

    <?xml version="1.0" encoding="UTF-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke 
        android:width="1dp" 
        android:color="#505050"/>
    <corners 
        android:radius="7dp" />

    <padding 
        android:left="1dp"
        android:right="1dp"
        android:top="1dp"
        android:bottom="1dp"/>

    <solid android:color="#505050"/>

</shape>

试着用这个来实现它.您可能需要根据需要更改颜色值.

Just try to implement it with this. You might need to change the color value as per your requirement.

如果它不起作用,请告诉我.

Let me know if it doesn't work.

这篇关于如何使用 XML 样式在 Android 中创建自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆