Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 样式中的布局属性_Android_Android Layout_Android Styles - Fatal编程技术网

Android 样式中的布局属性

Android 样式中的布局属性,android,android-layout,android-styles,Android,Android Layout,Android Styles,我有一段时间进退两难,不知道如何恰当地解决它。我想使用DRY(不要重复),但不要在样式中应用不好的做法(例如在样式中设置布局属性) 这是我的案子 为了将文本样式封装到我的项目中,我通常使用以下方法: 我有一种称为Wrap\u Content <style name="WrapContent"> <item name="android:layout_width">wrap_content</item> <item name="android

我有一段时间进退两难,不知道如何恰当地解决它。我想使用DRY(不要重复),但不要在样式中应用不好的做法(例如在样式中设置布局属性)

这是我的案子

为了将文本样式封装到我的项目中,我通常使用以下方法:

我有一种称为
Wrap\u Content

<style name="WrapContent">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>
如您所见,
Tv
样式具有默认字体和文本颜色 例如,如果我想使用15sp的字体大小,我将应用以下样式:

<style name="Tv.15">
    <item name="android:textSize">15sp</item>
</style>
如果在任何情况下,我想改变任何属性,我只需要从我调用它的地方覆盖它

进退两难的是,我把
文本外观
样式与
布局
样式混为一谈。我想把这个分开。。。但我并没有解决主要的问题,我正在设置它的布局属性,我只知道它自己的视图,而不知道它的容器

但根本无法说服我的是做这样的事情:

<style name="Tv">
    <item name="android:fontFamily">@font/font_foo</item>
    <item name="android:textColor">@color/color_foo</item>
</style>

<style name="Tv.15">
    <item name="android:textSize">15sp</item>
</style>

<TextView
    style="@style/Tv.15"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/foo"/>
<style name="WrapContent">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

<style name="TextAppearance">
    <item name="android:fontFamily">@font/font_foo</item>
    <item name="android:textColor">@color/color_foo</item>
</style>

<style name="TextAppearance.15">
    <item name="android:textSize">15sp</item>
</style>

<style name="Tv" parent="WrapContent">
    <item name="android:textAppearance">@style/TextAppearance</item>
</style>

<style name="Tv.15">
    <item name="android:textAppearance">@style/TextAppearance.15</item>
</style>

这会给系统增加一点复杂性,但会分割布局和textAppearance属性。此外,它允许对按钮、编辑文本等使用
文本外观
样式。

在我们最近的Android开发峰会上,我的两位同事就如何使用进行了讨论。我们建议您为视图组及其子视图使用主题,为更简单的视图使用样式。也许可以通过使用主题,然后为文本外观保留样式等来满足布局需求。除此之外,效能应该指导你如何构造你的样式对象。

如果你有一个
textAppearance
样式和一个
layout
样式,你就不能将它们应用到同一个
TextView
,因为每个
视图
只会使用一个
style=“@style/…”
属性。因此,不混合这两种属性似乎是不可能的。我不是设计大师,但最近我开始在某个
视图
在我的应用程序中扮演技术角色后声明样式-标签在整个应用程序中应该有一种样式,而标题则是另一种。你可以将这两种样式(文本外观和样式)应用到同一个视图@0X0nosugar。好吧,以防万一它是文本视图。但这不是问题…:-)我的意思是,不能将两个style=“@style/…”属性设置为同一个视图。因此,不可能同时将一个仅包含与布局相关属性的样式和一个仅包含与其外观相关属性的样式应用于同一视图。因此,尝试将这两个方面分开对android来说是行不通的,即使这可能是架构POVThanks,Isai的一个好主意!我已经看过录像了。太棒了!:-)。我已经开始应用他们提到的建议之一,即
?attr/color
。关于样式,我认为有其他方法可以添加新的样式层。我已经编辑了答案以显示它;-)你的东西很好。需要注意的是,由于您正在使用
parent
扩展
WrapContent
,因此应该考虑使用点符号使派生样式的名称更具描述性。可能类似于“WrapContent.textpearance.Tv”。因此,为了解释这意味着什么:如果您定义了一个
父项
,那么系统会忽略点符号,这只是您命名样式的方式。另外,如果应用程序中有多个文本外观,则
textpearance
可能不够描述性。(命名的片断可以很容易地记住发生的事情,一瞥“我喜欢它!谢谢,伊赛!”)——如果这个答案给了你所需的方向,请考虑接受它,以便帮助其他用户/寻求者。
<style name="Tv">
    <item name="android:fontFamily">@font/font_foo</item>
    <item name="android:textColor">@color/color_foo</item>
</style>

<style name="Tv.15">
    <item name="android:textSize">15sp</item>
</style>

<TextView
    style="@style/Tv.15"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/foo"/>
<style name="WrapContent">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

<style name="TextAppearance">
    <item name="android:fontFamily">@font/font_foo</item>
    <item name="android:textColor">@color/color_foo</item>
</style>

<style name="TextAppearance.15">
    <item name="android:textSize">15sp</item>
</style>

<style name="Tv" parent="WrapContent">
    <item name="android:textAppearance">@style/TextAppearance</item>
</style>

<style name="Tv.15">
    <item name="android:textAppearance">@style/TextAppearance.15</item>
</style>