简体   繁体   English

Android按钮样式编程

[英]Android Button Styling Programmatically

How do you programmatically add/remove style to an android button? 你如何以编程方式添加/删除样式到Android按钮? Is it possible to apply the styling at runtime? 是否可以在运行时应用样式?

I have two buttons that look like these 我有两个看起来像这样的按钮

----------   ----------
    | Button A | | Button B |
     ----------   ----------

what i wanted to do is when a button is clicked (lets say Button B), it runs some code, then changes the style of button B to something else (ie highlighted borders) and will be something like this: 我想要做的是当点击一个按钮(让我们说按钮B),它运行一些代码,然后将按钮B的样式更改为其他东西(即突出显示的边框),将是这样的:

----------    ==========
    | Button A | || Button B ||
     ----------    ==========

I know how to do the styling(ie create the style) in XML, all I want to know is how to apply the styles on runtime/using java code. 我知道如何在XML中进行样式设置(即创建样式),我想知道的是如何在运行时/使用java代码应用样式。

Let's do some code for you case...:) For applying style to your view (button in this case) dynamically is you have to do the following in your layout folder (res/layout). 让我们为你做一些代码... :)为了动态地将样式应用到你的视图(在这种情况下是按钮),你必须在你的布局文件夹(res / layout)中执行以下操作。

I named it as, buttonstyle.xml 我把它命名为buttonstyle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#449def"/>
            <stroke android:width="1dp" android:color="#2f6699"/>
            <corners android:radius="3dp"/>
            <padding android:left="10dp" android:top="10dp" android:right="10dp"
                     android:bottom="10dp"/>
        </shape>
    </item>

    <item>
        <shape>
            <gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270"/>
            <stroke android:width="1dp" android:color="#2f6699"/>
            <corners android:radius="4dp"/>
            <padding android:left="10dp" android:top="10dp" android:right="10dp"
                     android:bottom="10dp"/>
        </shape>
    </item>

</selector>

Now apply style to your button, add the following code to onCreate() method of your activity.. 现在将样式应用于您的按钮,将以下代码添加到您的活动的onCreate()方法中。

Button transferBtn = new Button(this);
transferBtn.setText("Test Example");
transferBtn.setId(R.string.transferBtn);
transferBtn.setBackgroundResource(R.layout.buttonstyle);

You can't apply xml-defined styles in runtime (from code). 您不能在运行时(从代码)应用xml定义的样式。 If you want to change background and font style when button is clicked (pressed) you should create selector which defines what background to use for normal button or for clicked state. 如果要在单击(按下)按钮时更改背景和字体样式,则应创建选择器 ,该选择器定义用于普通按钮或单击状态的背景。

If selector is not what you want, you should manually set every button property to desired value via button's setXXX method of Button class. 如果选择器不是您想要的,您应该通过按钮的Button类的setXXX方法手动将每个按钮属性设置为所需的值。

PS You can swap old button for a new another one inflated from xml with different style. PS你可以用旧按钮换一个新的另一个从xml膨胀的不同风格的按钮。 But this is not a good way I suppose... 但这不是我认为的好方法......

正如我在另一个帖子中回答的那样,您可以像某些人建议的那样以编程方式设置背景,或者如果您使用支持库,则可以以编程方式设置样式(如我在此建议的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM