【问题标题】:Dynamically change button style动态改变按钮样式
【发布时间】:2017-08-04 23:25:40
【问题描述】:

我正在开发一个动态创建整个布局的 Android 项目。现在我需要将按钮更改为圆角。所以我不能在 xml 中执行此操作,但必须在代码中执行此操作。有人知道怎么做吗?

【问题讨论】:

标签: android button


【解决方案1】:

使用

button.setBackgroundResource(R.drawable.custom_rouded_button_background);

custom_rouded_button_background.xml 在您的可绘制文件夹中定义。

【讨论】:

    【解决方案2】:

    您需要一个基于 xml 的可绘制资源,如下所示:

    <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke android:width="1dp"
        android:color="#000000" />
    
    <corners android:radius="5dp" />
    
    <solid android:color="#FFFFFF" />
    

    并更改按钮的背景。使其成为背景。

    【讨论】:

      【解决方案3】:
      <?xml version="1.0" encoding="utf-8"?>
      <shape 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         android:shape="rectangle">
         <corners
               android:topLeftRadius="0dp"
               android:topRightRadius="0dp"
               android:bottomLeftRadius="0dp"
               android:bottomRightRadius="0dp"/>
         <solid
            android:color="#0C094F"/>
         <size
           android:width="dp"
           android:height="62dp"/>
         <stroke
              android:width="3dp"
              android:color="#878787"/>
      </shape>
      

      【讨论】:

        猜你喜欢
        • 2014-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-05
        • 2014-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多