【问题标题】:How to change the style of a single view within a fragment如何更改片段中单个视图的样式
【发布时间】:2020-07-13 07:51:38
【问题描述】:

目标:我试图在一个片段中应用两个不同的主题。一个用于片段的整体 xml,另一个用于该片段 xml 中的特定视图。

原因/问题:原因是似乎无法使用 MaterialComponents 将浮动操作按钮的整个背景更改为矢量,但它确实适用于 appCompat。

最初,我尝试使用 style="..." 更改 xml 中的主题,如下所示,但它似乎恢复到清单中声明的​​主题,即 AppTheme1。我知道这一点,因为我通过切换那里声明的主题对其进行了测试。也就是说,当我将它切换到 AppTheme2 时,向量在 FAB 中正确加载,但它在其他地方崩溃,因为我在整个应用程序中都有 MaterialComponents。

解决方案:我认为明显的解决方案是更改相关浮动操作按钮的主题,但我不知道该怎么做。非常感谢任何帮助。谢谢!

         <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/nationality"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_margin="10dp"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/flag_united_states_of_america"
                app:borderWidth="0dp"
                style="@style/AppTheme2"
                app:maxImageSize="56dp" />

主题:

<style name="AppTheme1"  parent="Theme.MaterialComponents.Light.NoActionBar" >

&&

<style name="AppTheme2"  parent="Theme.AppCompat.Light.NoActionBar" >

使用材料组件:

使用 AppCompat:

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fabtest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

因此,当主题从 AppCompat 更改为 Material Components 时,图像资源不再正确应用于 Floating Action Button。所以我只想为浮动操作按钮应用 AppCompat,但保持材质组件为主要样式。

【问题讨论】:

  • 晶圆厂的风格不是主题。真的不清楚你想要实现什么
  • @GabrieleMariotti 好的,我会改写。

标签: android android-layout android-theme android-styles floating-action-button


【解决方案1】:

在您的情况下,您可以使用:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    app:tint="@null"
    app:srcCompat="@drawable/flag_united_states_of_america"
    ..>

Material Components 中的FloatingActionButton 使用应用主题中定义的colorOnSecondary 属性为其图标着色(默认值为#000000)。如果您不想为图标着色,则必须使用 app:tint="@null"

一般来说,如果您想在您的 FAB 中使用 自定义样式,您可以使用:

  <com.google.android.material.floatingactionbutton.FloatingActionButton
      style="@style/MyCustomFab"
      ..>

与:

   <style name="MyCustomFab" parent="@style/Widget.MaterialComponents.FloatingActionButton>
      .....
      <item name="backgroundTint">@color/....</item>
      <item name="tint">@color/....</item>
   </style>

如果您想覆盖应用主题中定义的属性,您可以使用:

  <com.google.android.material.floatingactionbutton.FloatingActionButton
      android:theme="@style/fab_themeoverlay"
      ..>

与:

   <style name="fab_themeoverlay" parent="@style/Widget.MaterialComponents.FloatingActionButton>
      <item name="colorPrimary">@color/...</item>
   </style>

【讨论】:

    【解决方案2】:

    如果你想改变按钮的主题,这会影响它的膨胀方式,你应该使用android:theme而不是android:style

    【讨论】:

      【解决方案3】:

      Ben P. 和 Gabrielle 都是半正确的。答案需要通过以下方式改变主题:

      android:theme="@style/SecondTheme"
      

      并将色调设置为 null:

      app:tint="@null"
      

      在这种情况下,两者都是必需的

      总而言之,给定两个主题,如果您想将其整个背景更改为矢量,而您的主要主题是应用程序清单中引用的 MaterialComponents,那么这是 FloatingActionButton 的正确 XML:

      <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                app:borderWidth="0dp"
      
                app:srcCompat="@drawable/your_vector"
                app:tint="@null"
                android:theme="@style/SecondTheme"
                app:maxImageSize="56dp" />
      

      感谢 Gabrielle 和 Ben 的帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多