drawableLeft/Right 等不适用于 Theme.MaterialComponents
drawableLeft/Right etc. doesn't work with Theme.MaterialComponents
使用 Activity Theme.MaterialComponents....
的主题时,如果我想使用 drawableLeft/Right
等按钮,它们不会显示。但是,如果我使用主题 Base.Theme.AppCompat
,它们会按预期工作。
这是按钮:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test Button"
android:drawableLeft="@drawable/ic_envelope"/>
这就是主题 Base.Theme.MaterialComponents
的样子
这是 Base.Theme.AppCompat
如何让 drawableLeft
使用 Base.Theme.MaterialComponents
主题?
只需将 MaterialButton
与 app:icon
属性一起使用:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
app:icon="@drawable/ic_add_24px"
../>
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.Icon"
app:icon="@drawable/ic_add_24px"
../>
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
app:icon="@drawable/ic_add_24px"
../>
使用app:iconGravity
配置start|end|textStart|textEnd
.
<com.google.android.material.button.MaterialButton
app:iconGravity="end"
../>
使用 Activity Theme.MaterialComponents....
的主题时,如果我想使用 drawableLeft/Right
等按钮,它们不会显示。但是,如果我使用主题 Base.Theme.AppCompat
,它们会按预期工作。
这是按钮:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test Button"
android:drawableLeft="@drawable/ic_envelope"/>
这就是主题 Base.Theme.MaterialComponents
的样子
这是 Base.Theme.AppCompat
如何让 drawableLeft
使用 Base.Theme.MaterialComponents
主题?
只需将 MaterialButton
与 app:icon
属性一起使用:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
app:icon="@drawable/ic_add_24px"
../>
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.Icon"
app:icon="@drawable/ic_add_24px"
../>
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
app:icon="@drawable/ic_add_24px"
../>
使用app:iconGravity
配置start|end|textStart|textEnd
.
<com.google.android.material.button.MaterialButton
app:iconGravity="end"
../>