当前位置: 首页 > 知识库问答 >
问题:

如何以编程方式将selectableItemBackground添加到ImageButton?

孟昊空
2023-03-14

AndroidR、 属性。selectableItemBackground存在,但如何通过编程将其添加到ImageButton?

此外,我如何在文档中找到答案?这里提到过,但我看不到任何关于它实际如何使用的解释。实际上,我似乎很少发现文档有用,但我希望这是我的错,而不是文档的错。

共有3个答案

井镜
2023-03-14

这适用于我的TextView

// Get selectable background
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true);

clickableTextView.setClickable(true);
clickableTextView.setBackgroundResource(typedValue.resourceId);

因为我使用AppCompat库,所以我使用R.attr.selectableItembackground而不是android。R.attr.selectableItembackground

我认为类型值。resourceId(resourceId)保存来自selectableItemBackground(selectableItemBackground)的所有可绘图项,而不是使用TypeArray(索引,defValue)或TypeArray(索引)或TypeArray(索引),它们仅在给定的索引处检索可绘图项。

葛鸿轩
2023-03-14

如果您使用的是AppCompat,您可以使用以下代码:

int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = context.obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
view.setBackgroundResource(backgroundResource);
typedArray.recycle();
晋安国
2023-03-14

这里有一个使用答案的示例:如何在代码中获取attr引用?

    // Create an array of the attributes we want to resolve
    // using values from a theme
    // android.R.attr.selectableItemBackground requires API LEVEL 11
    int[] attrs = new int[] { android.R.attr.selectableItemBackground /* index 0 */};

    // Obtain the styled attributes. 'themedContext' is a context with a
    // theme, typically the current Activity (i.e. 'this')
    TypedArray ta = obtainStyledAttributes(attrs);

    // Now get the value of the 'listItemBackground' attribute that was
    // set in the theme used in 'themedContext'. The parameter is the index
    // of the attribute in the 'attrs' array. The returned Drawable
    // is what you are after
    Drawable drawableFromTheme = ta.getDrawable(0 /* index */);

    // Finally free resources used by TypedArray
    ta.recycle();

    // setBackground(Drawable) requires API LEVEL 16, 
    // otherwise you have to use deprecated setBackgroundDrawable(Drawable) method. 
    imageButton.setBackground(drawableFromTheme);
    // imageButton.setBackgroundDrawable(drawableFromTheme);
 类似资料:
  • Spring非常出色地为注释提供了一个属性。我希望以编程方式提供带有时区的方法。 例如,假设我想在当地时间午夜打开洛杉矶和芝加哥大楼的灯。我想从我的存储库中获取建筑物。 时区可以在运行时提供还是以其他方式注入? 比如: 我知道方法不能接受任何参数,所以以这种方式提供时区是不行的。

  • 我正在使用SpringDoc,并试图以编程方式向OpenApi添加一个模式,但没有成功。 mySchema的描述没有添加到我在生成的YAML文件中看到的模式列表中,如果我试图引用它:

  • 我正在尝试在Android上添加Wifi网络,我想知道如何连接到不广播其SSID的Wifi网络(它是否有空SSID或带有\0s的清晰SSID)。 这是我目前用于广播其SSID的Wifi网络的内容:

  • 问题内容: 对于RealityKit的文档包括结构:,,和用于添加材料到。 或者,您可以在模型中加载带有附加材料的模型。 我想以编程方式添加自定义材料/纹理。如何在不向Reality Composer或其他3D软件的模型中添加材料的情况下即时实现这一目标? 问题答案: 已更新:2020年6月23日 。 目前,RealityKit 2.0中有4种类型的材料 : 简单材质 不发光材料 咬合材料 Vid

  • 问题内容: 我试图自定义现有的JS库,而不修改原始JS代码。这段代码将加载一些我可以访问的外部JS文件,而我想做的就是更改原始文件中包含的功能之一,而无需将整个内容复制并粘贴到第二个JS文件中。 因此,例如,禁区JS可能具有以下功能: 我希望能够以某种方式在该函数中追加或添加一些JS代码。原因主要是在原始的不可触摸的JS中,该功能非常庞大,如果该JS得到更新,则我用它覆盖的功能将过时。 我不确定这

  • 问题内容: 尽管我看过很多类似的问题,但没有找到明确的答案。使用Servlet Spec 2.5,是否可以以编程方式添加Servlet过滤器和映射?首选位置在Servlet.init()或ServletContextListener.contextInitialized()中。 问题答案: 不,不是通过标准的Servlet 2.5 API。这是在Servlet 3.0 中引入的。最好的选择是创建一