我的主题定义为
<style name="AppThemeRed" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimaryRed</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkRed</item>
<item name="colorAccent">@color/colorAccentRed</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在我的 XML 布局中,我正在做
<android.support.design.widget.AppBarLayout
android:background="?attr/colorPrimary"
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
Run Code Online (Sandbox Code Playgroud)
每当我更改任何主题时,colorPrimary 都会发生变化
但是,如果我在 drawable 中添加了相同的东西,例如
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
它因无法膨胀视图而崩溃,该视图的背景设置为 @drawabe/xxxx
如何在我的 XML drawable 中定义主题颜色属性
无论如何要在布局xml文件中设置“android.support.v7.widget.Toolbar”一半的预定义属性的高度,例如“?android:attr/actionBarSize”?事实上,我希望我的工具栏的高度是这样的:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height=("?android:attr/actionBarSize")/2
>
Run Code Online (Sandbox Code Playgroud) android android-layout android-view android-resources android-styles
我在styles.xml 中定义了以下样式:
<style name="NotificationTitle"
parent="android:TextAppearance.Material.Notification.Title" />
Run Code Online (Sandbox Code Playgroud)
如您所见,它扩展了 Android 中定义的以下默认样式:
<style name="TextAppearance.Material.Notification.Title">
<item name="textColor">@color/notification_primary_text_color_light</item>
<item name="textSize">@dimen/notification_title_text_size</item>
</style>
Run Code Online (Sandbox Code Playgroud)
现在,回到我的styles.xml 文件,我想创建以下自定义样式,其“android:tint”属性设置为与父样式中定义的“textColor”相同的值。我试过这样的事情:
<style name="NotificationIcon" >
<item name="android:tint">@style/NotificationTitle.textColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但是我不能像那样引用“textColor”,它无法解析符号。那么如何从我的自定义样式中引用另一种样式中定义的另一个属性呢?如果不可能,最好的选择是什么?
我的主要应用主题父主题是 MaterialComponents 主题。我更改了颜色,但材质组件(MaterialButton、InputLayout、EditText)没有使用我的强调色(它们使用了一些蓝色,我声明的强调色是蓝绿色)有什么问题?处理主题化材质组件的最佳方法是什么?
我的主题:
<style name="AppTheme" parent="Theme.MaterialComponents">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
编辑:将主题更改为 Theme.MaterialComponents.Bridge 并不能解决该问题。
我有一个按钮可以启用/禁用复选框,但由于它的颜色,我看不到禁用的复选框(我的样式背景颜色必须与禁用的复选框颜色相同)。如何更改禁用复选框的颜色,并确保在复选框再次启用时颜色恢复为“启用”颜色?
这就是我的意思:
有问题的复选框很简单:
<CheckBox
android:id="@+id/settings_hide_nsfw_thumbnails"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud) 以“android”为前缀的属性工作正常,但如何添加以其他内容为前缀的属性?
例如,如何将以下两个属性添加到下面的样式中?
app:chipCornerRadius="20dp"
app:textStartPadding="8dp"
<style name="Chip" parent="Base.TextAppearance.AppCompat.Small">
<item name="android:textSize">@dimen/smallTextSize</item>
<item name="android:textColor">@color/black</item>
</style>
Run Code Online (Sandbox Code Playgroud) 我在 中添加了 2 个选项卡TabLayout,以下是我的代码。
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
style="@style/AppTabLayout"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center"
app:tabBackground="@drawable/tab_selector_statistics"
android:background="@drawable/tab_statistics"
app:tabContentStart="24dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/black"
app:layout_constraintBottom_toBottomOf="@+id/view5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view5"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
Run Code Online (Sandbox Code Playgroud)
我得到以下输出。
但我希望标签占据屏幕的整个宽度,如下所示。
以下是我AppTabLayout在styles.xml文件中定义的样式。
<style name="AppTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@null</item>
<item name="tabIndicatorHeight">1dp</item>
<item name="tabPaddingStart">16dp</item>
<item name="tabPaddingEnd">16dp</item>
<item name="tabSelectedTextColor">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud) 我正在使用新的MaterialComponents,我的应用程序主题是:
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryDarkColor</item>
<item name="colorAccent">@color/secondaryColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)
如何更改 ActionBar 的背景颜色
我知道这个问题,但它已经过时并且不使用MaterialComponents
我不想将 Toolbar 添加到每个屏幕只是为了更改背景颜色。
android android-layout android-actionbar android-styles material-components-android
我尝试使用style它shapeAppearance:
<style name="ShapeAppearance.Button" parent="ShapeAppearance.MaterialComponents">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopLeft">@dimen/button_corner_radius</item>
<item name="cornerSizeTopRight">5dp</item>
<item name="cornerSizeBottomRight">@dimen/button_corner_radius</item>
<item name="cornerSizeBottomLeft">5dp</item>
</style>
<dimen name="button_corner_radius">40dp</dimen>
Run Code Online (Sandbox Code Playgroud)
我像这样在我的 MaterialButton 中应用样式:
app:shapeAppearance="@style/ShapeAppearance.Button"
Run Code Online (Sandbox Code Playgroud)
结果是:
现在,我尝试放置一个线性渐变:
例如 :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#DF2D48"
android:centerColor="#D72D46"
android:endColor="#AC2139"
android:angle="360" />
</shape>
Run Code Online (Sandbox Code Playgroud)
我在我的形状样式中使用它:
<item name="android:background">@drawable/test</item>
Run Code Online (Sandbox Code Playgroud)
但结果是一样的。为什么 ?
android android-button android-styles material-components material-components-android
android ×10
android-styles ×10
material-components-android ×2
android-view ×1
java ×1
themes ×1