【发布时间】: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