Android 中带有背景颜色的圆角按钮

发布于 2024-10-25 18:05:03 字数 71 浏览 5 评论 0原文

我需要在android中做圆角按钮并改变背景颜色。

我怎么能这么做呢?

示例链接/代码非常感谢。

I need to do round cornered button with background color change in android.

How could i do that?

Example link/code is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北恋 2024-11-01 18:05:03

您想要使用 Android 的 Shape Drawables。
http://developer.android.com/guide/topics/resources /drawable-resource.html#Shape

drawable/cool_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="@dimen/corner_radius" />
    <gradient
        android:angle="270"
        android:startColor="@color/almost_white"
        android:endColor="@color/somewhat_gray"
        android:type="linear" />
</shape>

然后您必须从这些形状可绘制对象创建一个“选择器”可绘制对象。这允许您根据状态使按钮显示不同。 IE:按下、聚焦等。
http://developer.android.com/guide/topics/resources /drawable-resource.html#StateList

drawable/cool_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/cool_inner_press_bottom" />
    <item android:state_focused="true" android:state_enabled="true"
        android:state_window_focused="true"
        android:drawable="@drawable/cool_inner_focus_bottom" />
    <item
         android:drawable="@drawable/cool_button_background" />
</selector>

额外好处:您可能想要为按钮创建一个样式,以便它们在整个程序中保持一致。您可以省略这一步,只需设置按钮的 android:background="@drawable/cool_button" 即可。

value/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyCoolButton">
        <item name="android:background">@drawable/cool_button_background</item>
    </style>
</resources>

最后是按钮!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/appwidget_bg">
    <Button
        android:id="@+id/btnAction"
        android:layout_width="wrap_content"
        android:layout_weight="wrap_content"
        style="@style/CoolButton"
        />
</LinearLayout>

You want to use Android's Shape Drawables.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

drawable/cool_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="@dimen/corner_radius" />
    <gradient
        android:angle="270"
        android:startColor="@color/almost_white"
        android:endColor="@color/somewhat_gray"
        android:type="linear" />
</shape>

Then you'd have to create a "selector" drawable from those shape drawables. This allows you to make the button appear different depending on the state. IE: Pressed, focused, etc.
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

drawable/cool_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/cool_inner_press_bottom" />
    <item android:state_focused="true" android:state_enabled="true"
        android:state_window_focused="true"
        android:drawable="@drawable/cool_inner_focus_bottom" />
    <item
         android:drawable="@drawable/cool_button_background" />
</selector>

Bonus: You might want to create a style for the button so you can have them be consistent throughout the program. You can cut this step out and just set the button's android:background="@drawable/cool_button".

values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyCoolButton">
        <item name="android:background">@drawable/cool_button_background</item>
    </style>
</resources>

Finally, the button!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/appwidget_bg">
    <Button
        android:id="@+id/btnAction"
        android:layout_width="wrap_content"
        android:layout_weight="wrap_content"
        style="@style/CoolButton"
        />
</LinearLayout>
变身佩奇 2024-11-01 18:05:03

导入 PorterDuff 并使用 setColorFilter() 如下

import android.graphics.PorterDuff.Mode;

Button btn = (Button) findViewById(R.id.myButton); 
btn.getBackground().setColorFilter(Color.GRAY, Mode.MULTIPLY);

Import PorterDuff and use setColorFilter() as follows

import android.graphics.PorterDuff.Mode;

Button btn = (Button) findViewById(R.id.myButton); 
btn.getBackground().setColorFilter(Color.GRAY, Mode.MULTIPLY);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文