安卓按钮的程序化样式[英] Android Button Styling Programatically

本文是小编为大家收集整理的关于安卓按钮的程序化样式的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何以android按钮编程地添加/删除样式?是否可以在运行时应用样式?

我有两个看起来像这些

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

的按钮

我想做的是,点击一个按钮时(让说按钮b),它运行一些代码,然后将按钮b的样式更改为else(即突出显示的边框),并将是如此:

我知道如何在XML中进行样式(即创建样式),所有我想知道的是如何在运行时/使用Java代码应用样式.

推荐答案

让我们为你做一些代码...... :) 为了动态地将样式应用于视图(在此情况下的按钮),您必须在布局文件夹中执行以下操作(RES/布局).

我将其命名为, 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>

现在将样式应用于按钮,将以下代码添加到您的活动中的oncreate()方法.

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

本文地址:https://www.itbaoku.cn/post/102597.html

问题描述

How do you programatically add/remove style to an android button? 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 (i.e highlighted borders) and will be something like this:

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

I know how to do the styling(i.e create the style) in XML, all I want to know is how to apply the styles on runtime/using java code.

推荐答案

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).

I named it as,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..

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