如何使用XML样式在Android中创建自定义按钮
我想通过使用XML样式来制作这种按钮[相同的背景和文本]颜色 这只是一个例子,我想写一些其他的文本,比如:关于我的 我仍然在使用Photoshop中由设计师创建的按钮如何使用XML样式在Android中创建自定义按钮,android,android-widget,Android,Android Widget,我想通过使用XML样式来制作这种按钮[相同的背景和文本]颜色 这只是一个例子,我想写一些其他的文本,比如:关于我的 我仍然在使用Photoshop中由设计师创建的按钮 <ImageButton android:id="@+id/imageButton5" android:contentDescription="AboutUs" android:layout_width="wrap_content" android:l
<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" />
注意:我需要各种尺寸和形状的这种按钮
我不想在我的Android应用程序中使用任何图像,我只想使用XML
1.将此项添加到您的绘图工具中
2.将按钮设置为背景查看一下,肯定会对您有所帮助。
有很多例子,请在网上搜索
例:风格
<style name="Widget.Button" parent="android:Widget">
<item name="android:background">@drawable/red_dot</item>
</style>
@可拉伸/红点
您可以使用选择器而不是红点
红点:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#f00"/>
<size android:width="55dip"
android:height="55dip"/>
</shape>
按钮:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
style="@style/Widget.Button"
android:text="Button" />
您是否尝试过为任何按钮创建背景形状 请查看以下内容: 下面是与按钮图像分离的图像 现在,将其放入android:src“source”的ImageButton中,如下所示: 现在,只需创建ImageButton的形状,使其具有黑色着色器背景
android:background="@drawable/button_shape"
按钮形状是可绘制资源中的xml文件:
<?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>
试着用这个来实现它。您可能需要根据需要更改颜色值
如果不起作用,请告诉我。从androidcookbook.com上“Adrián Santalla”所写的食谱粘贴的副本: 1。创建表示按钮状态的XML文件 将xml创建到名为“button.xml”的可绘制文件中,以命名按钮状态:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/button_focused" />
<item
android:state_enabled="true"
android:drawable="@drawable/button_enabled" />
</selector>
2。创建表示每个按钮状态的XML文件
为四个按钮状态中的每一个创建一个xml文件。所有这些都应该放在drawables文件夹下。让我们按照button.xml文件中设置的名称进行操作
按钮u enabled.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#00CCFF"
android:centerColor="#0000CC"
android:endColor="#00CCFF"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#F2F2F2"
android:centerColor="#A4A4A4"
android:endColor="#F2F2F2"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
button_focused.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#F7D358"
android:centerColor="#DF7401"
android:endColor="#F7D358"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
按钮按下。xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#0000CC"
android:centerColor="#00CCFF"
android:endColor="#0000CC"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
按钮u disabled.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#00CCFF"
android:centerColor="#0000CC"
android:endColor="#00CCFF"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#F2F2F2"
android:centerColor="#A4A4A4"
android:endColor="#F2F2F2"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
3。创建表示按钮样式的XML文件
创建了上述文件后,就可以创建应用程序按钮样式了。现在,您需要创建一个名为styles.XML(如果您还没有)的新XML文件,在其中可以将更多自定义样式包含到de values目录中
此文件将包含应用程序的新按钮样式。您需要在其中设置新的按钮样式功能。请注意,其中一个特性,即新样式的背景,应该通过对第一步中创建的按钮(button.xml)可绘制的引用进行设置。为了引用新的按钮样式,我们使用name属性
下面的示例显示了styles.xml文件的内容:
<resources>
<style name="button" parent="@android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#FFFFFFFF</item>
<item name="android:shadowColor">#FF000000</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">-1</item>
<item name="android:shadowRadius">0.2</item>
<item name="android:textSize">16dip</item>
<item name="android:textStyle">bold</item>
<item name="android:background">@drawable/button</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
</resources>
中心|垂直|中心|水平
#FFFFFFFF
#FF000000
0
-1
0.2
16倾角
大胆的
@可拉拔/按钮
真的
真的
4。使用您自己的自定义应用程序主题创建XML
最后,您需要覆盖默认的Android按钮样式。为此,您需要在values目录中创建一个名为themes.XML(如果您还没有)的新XML文件,并覆盖默认的Android按钮样式
下面的示例显示了themes.xml的内容:
<resources>
<style name="YourApplicationTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:buttonStyle">@style/button</item>
</style>
</resources>
@样式/按钮
希望你们能像我在寻找定制按钮时一样幸运。享受。
如果您想定制按钮设计,需要做两件事 第一名是: 在drawable文件夹中创建xml资源文件(例如:btn_shape_rectangle.xml) 然后复制并粘贴代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:shape="rectangle">
<solid
android:color="#fff"/>
<stroke
android:width="1dp"
android:color="#000000"
/>
<corners android:radius="10dp" />
</shape>
第二个是转到要实施此设计的布局按钮。把它连接起来。
例子:
android:background=“@drawable/btn\u shape\u rectangle”
你可以改变形状颜色半径你想要的设计可以做什么
希望它能起作用,对你有帮助。Happy Coding为我们提供这样的图像,并将其设置为imageView的背景。是的,我知道,但我需要在我的系统中实现相同的背景和文本颜色code@Sneha你的意思是你在问题中给出的图像的确切颜色吗?@Sneha你有互联网连接,请使用它。您可以找到该背景色的颜色代码。使用Gru的答案创建可使用该颜色绘制的图像。搜索符合您要求的字体并应用于该按钮。您的链接指向xamarin。有一些差异可能会造成混淆(比如,这甚至不是Java代码)。我知道如何使用形状,但我需要在代码中实现相同的背景和文本颜色。OK,得到了一个更好的答案,肯定会帮助其他人。关于回答一个老话题,我觉得当我在寻找它的时候,其他人也可能会这样做。主题越完整,开发人员就越快找到解决问题的方法,至少我这样认为。无论如何,谢谢。回答得很好,但你知道为什么会忽略填充选项吗?仅供参考-兄弟,你可能想将链接的https更改为http-因为该站点不再使用安全协议。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:shape="rectangle">
<solid
android:color="#fff"/>
<stroke
android:width="1dp"
android:color="#000000"
/>
<corners android:radius="10dp" />
</shape>