【问题标题】:Change ImageButton states background color on runtime在运行时更改 ImageButton 状态背景颜色
【发布时间】:2013-10-04 11:10:52
【问题描述】:

我需要在运行时从选择器中更改颜色,并且需要将其应用于 4 个不同的按钮。

这是 ImageButton 代码:

<ImageButton
   android:id="@+id/buttonAgenda"
   android:layout_width="100dp"
   android:layout_height="100dp"           
   android:background="@drawable/drawable_button_states_corners"
   android:src="@drawable/boton_agenda_1"/>

drawable_button_states_corners:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"> 
        <solid android:color="@color/galicia2"/>    
        <corners 
            android:bottomRightRadius="10dp"
            android:bottomLeftRadius="10dp" 
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp"/> 
    </shape>         
</item>
<item android:state_enabled="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"> 
        <solid android:color="@color/galicia"/>    
        <corners android:bottomRightRadius="10dp"
            android:bottomLeftRadius="10dp" 
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp"/> 
    </shape>   
</item>
</selector>

我需要更改两种纯色,但我不知道如何访问它们。我已经尝试访问背景的图像按钮,但没有任何反应。

【问题讨论】:

    标签: android background runtime imagebutton


    【解决方案1】:

    创建另一个具有所需颜色的 state_corners 并根据需要更改可绘制对象

    【讨论】:

    • 如何更改可绘制对象?我正在设置背景,这是我设置可绘制对象的地方,但它不会改变,事实上,它正在失去默认的可绘制对象
    【解决方案2】:

    您无法获取 StateListDrawable 的状态,因此无法对其进行修改(除非您使用默认 ctor 和 addState() 方法创建了 SLD),但您始终可以创建新的 SLD 和 addState()s

    【讨论】:

      【解决方案3】:

      据我所知,要实现您想要的,您必须在运行时创建可绘制的形状,然后您可以创建状态列表以设置为背景。您可以使用此代码在运行时创建可绘制的形状

      ShapeDrawable footerBackground = new ShapeDrawable();
      
      // The corners are ordered top-left, top-right, bottom-right,
      // bottom-left. For each corner, the array contains 2 values, [X_radius,
      // Y_radius]
      float[] radii = new float[8];
      radii[0] = activity.getResources().getDimension(R.dimen.footer_corners);
      radii[1] = activity.getResources().getDimension(R.dimen.footer_corners);
      
      radii[2] = activity.getResources().getDimension(R.dimen.footer_corners);
      radii[3] = activity.getResources().getDimension(R.dimen.footer_corners);
      
      footerBackground.setShape(new RoundRectShape(radii, null, null));
      
      int color = ((Application) activity.getApplication()).getColor();
      
      footerBackground.getPaint().setColor(color);
      
      views.setBackgroundDrawable(footerBackground);
      

      这部分取自已接受的答案,所以发布Android: Change Shape Color in runtime

      请参阅有关在运行时创建颜色状态列表的文档http://developer.android.com/reference/android/content/res/ColorStateList.html

      【讨论】:

        猜你喜欢
        • 2015-09-26
        • 2019-03-07
        • 2019-11-20
        • 2016-10-16
        • 1970-01-01
        • 1970-01-01
        • 2015-10-09
        • 1970-01-01
        • 2022-12-14
        相关资源
        最近更新 更多