Android:通过使用主题,我是否只能为每个属性拍摄一次? Android 中有没有类似 CSS 的东西?

发布于 2024-09-24 03:47:17 字数 452 浏览 2 评论 0原文

这里他们写道:

要将样式定义应用为主题,您必须将该样式应用于 Android 清单中的活动或应用程序。当您这样做时,该视图中的每个视图 活动或申请将适用 它支持的每个属性。为了 例如,如果您应用 CodeFont 样式从前面的示例改为 Activity,然后是所有 View 元素 支持文本样式属性 应用它们。

那么,当我将 TextColor 设置为红色时,所有带有文本的元素是否都会更改为红色?没有像 CSS 样式表中那样强大的说明符吗?我只是认为样式(style="@style/CodeFont")的概念是可行的。

Here they write:

To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. When you do so, every View within the
Activity or application will apply
each property that it supports. For
example, if you apply the CodeFont
style from the previous examples to an
Activity, then all View elements that
support the text style properties will
apply them.

So, when I set TextColor to Red, do ALL Elements with Text change to red?? Arent there powerful specifier like in CSS style-sheets? I just see the concept of styles (style="@style/CodeFont") practicable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无可置疑 2024-10-01 03:47:17

为了响应您的评论,您可能会通过属性 textAppearance 找到您想要的内容。

在此处打开 theme.xml 文件:
https:// android.googlesource.com/platform/frameworks/base/+/froyo-release/core/res/res/values/themes.xml

如果您查找 textAppearance,您会注意到各种条目,例如:

<style name="Theme">
    <item name="textAppearanceButton">@android:style/TextAppearance.Widget.Button</item>
    ...
    <item name="panelTextAppearance">?android:attr/textAppearanceInverse</item>
    ...

这允许您在某种程度上,可以在整个应用程序中设置组件的样式,如 Maragues 示例所示。

因此,虽然不可能完全控制列表视图、滚动视图等内所有子元素的样式,但对控制某些基于文本的组件(例如 TextView 和/或按钮)的样式的支持有限。

In response to your comments you might find what your looking for with the property textAppearance.

Open the themes.xml file here:
https://android.googlesource.com/platform/frameworks/base/+/froyo-release/core/res/res/values/themes.xml

If you do a find for textAppearance you notice various entries such as:

<style name="Theme">
    <item name="textAppearanceButton">@android:style/TextAppearance.Widget.Button</item>
    ...
    <item name="panelTextAppearance">?android:attr/textAppearanceInverse</item>
    ...

This allows you to some extent to style Components throughout your application as shown in the Maragues example.

So whilst its not possible to fully control the style of all child elements withint say a listview, scroll view etc, there is some limited support for controlling the styles some Text based components such as the TextView and or Button.

纸短情长 2024-10-01 03:47:17

如果您阅读了引用的文本下方:

要为应用程序的所有活动设置主题,请打开 AndroidManifest.xml 文件并编辑标签以包含带有样式名称的 android:theme 属性

如果您希望主题仅应用于应用程序中的一个 Activity,请改为将 android:theme 属性添加到标记中。

之后,它描述了如何继承和重写属性,只需像CSS:

如果您喜欢某个主题,但想要调整它,只需将该主题添加为自定义主题的父主题即可。例如,您可以修改传统的对话框主题以使用您自己的背景图像,如下所示:

<style name="CustomTextView" parent="@android:style/Widget.TextView">
    <item name="android:background">@drawable/custom_background</item>
</style>

然后,从 CustomTextView 进行迭代

<style name="CustomTextView.RedText">
   <item name="android:textColor">@color/red</item> 
</style>

设计 XML 布局后,您可以继承主题并覆盖

<TextView
    android:id="@+id/example"

    style="@style/CustomTextView.RedText"

    android:textColor="@color/green" />

我认为您应该再次阅读的 属性您自己链接的页面,解释得很好:)

If you read just below the text you quoted:

To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the tag to include the android:theme attribute with the style name

If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the tag instead.

And a little bit after that, it describes how to inherit and override properties, just like CSS:

If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional dialog theme to use your own background image like this:

<style name="CustomTextView" parent="@android:style/Widget.TextView">
    <item name="android:background">@drawable/custom_background</item>
</style>

Then, to interit from CustomTextView

<style name="CustomTextView.RedText">
   <item name="android:textColor">@color/red</item> 
</style>

Once designing the XML layout, you can inherit a theme and override the properties

<TextView
    android:id="@+id/example"

    style="@style/CustomTextView.RedText"

    android:textColor="@color/green" />

I think you should read again the page you yourself linked, it's well explained :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文