如何通过样式设置 “app:backgroundTint”?

huangapple go评论79阅读模式
英文:

how to set "app:backgroundTint" via style?

问题

我想要替换:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:backgroundTint = "...",
    ...
        android:orientation="horizontal">

为:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        style="@style/My.Bg.Snackbar.DayNight"
    ...
        android:orientation="horizontal">


      <style name="My.Bg.Dark" parent="">
        <item name="app:backgroundTint">@color/og_background_dark</item>
      </style>

以及

      <declare-styleable name="My">
        <!-- The background color. -->
        <attr name="app:backgroundTint" format="color" />
      </declare-styleable>
    </resources>

但是我得到一个错误:

    error: resource app:attr/backgroundTint not found.
英文:

I want to replace:

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    app:backgroundTint = &quot;...&quot;
...
    android:orientation=&quot;horizontal&quot;&gt;

with

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    style=&quot;@style/My.Bg.Snackbar.DayNight&quot;
...
    android:orientation=&quot;horizontal&quot;&gt;

and

  &lt;style name=&quot;My.Bg.Dark&quot; parent=&quot;&quot;&gt;
    &lt;item name=&quot;app:backgroundTint&quot;&gt;@color/og_background_dark&lt;/item&gt;
  &lt;/style&gt;

and

  &lt;declare-styleable name=&quot;My&quot;&gt;
    &lt;!-- The background color. --&gt;
    &lt;attr name=&quot;app:backgroundTint&quot; format=&quot;color&quot; /&gt;
  &lt;/declare-styleable&gt;
&lt;/resources&gt;

but I get an error:

error: resource app:attr/backgroundTint not found.

答案1

得分: 2

你可以使用:

  <style name="My.Bg.Dark" parent="">
    <item name="backgroundTint">@color/og_background_dark</item>
  </style>
英文:

You can use:

  &lt;style name=&quot;My.Bg.Dark&quot; parent=&quot;&quot;&gt;
    &lt;item name=&quot;backgroundTint&quot;&gt;@color/og_background_dark&lt;/item&gt;
  &lt;/style&gt;

答案2

得分: 0

只使用这段代码

  &lt;style name=&quot;MyStyleName&quot; parent=&quot;&quot;&gt;
    &lt;item name=&quot;backgroundTint&quot;&gt;@color/green&lt;/item&gt;
  &lt;/style&gt;
英文:

just use this code

  &lt;style name=&quot;MyStyleName&quot; parent=&quot;&quot;&gt;
    &lt;item name=&quot;backgroundTint&quot;&gt;@color/green&lt;/item&gt;
  &lt;/style&gt;

huangapple
  • 本文由 发表于 2020年7月25日 20:16:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63088206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定