与使用 XML 样式系统相比,通过在 Java 中子类化来设置 Android 视图的样式有什么缺点?

What's the downside to styling Android Views by subclassing in Java as opposed to using the XML style system?

我一直在努力平衡臃肿的 xml 布局与难以掌握的 style.xml。所以我开始尝试为每种类型的输入字段创建一个新的小部件。例如,我有一个我在应用程序中随处使用的蓝色按钮,所以我正在制作一个 BlueButton,它只是在其构造函数中执行我想要的操作:

public class BlueButton extends AppCompatButton {
    public BlueButton(Context context) {        this(context,null);    }
    public BlueButton(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }
    public BlueButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundResource(R.drawable.bg_button_blue_gradient);
        setTextSize(COMPLEX_UNIT_SP,20);
        setAllCaps(true);
        setGravity(CENTER); setTextColor(ContextCompat.getColor(context,android.R.color.white));
    }
}

        <app.ui.widget.BlueButton
            android:id="@+id/myButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="My Happy Blue Button"
            />

这看起来很明显,我确定我遗漏了什么。这样做的缺点是什么?

您发布的 BlueButton class 可以改为样式资源:

<style name="BlueButton">
    <item name="android:background">@drawable/bg_button_blue_gradient</item>
    <item name="android:textSize">20sp</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:gravity">center</item>
    <item name="android:textColor">@android:color/white</item>
</style>

从你的问题看来你可能已经知道了这一点,但我这么说是为了我们可以将样式资源与你发布的 Java 代码进行比较。

在一种情况下,您更改了正在使用的标签:

<app.ui.widget.BlueButton ... />

在另一种情况下,您添加一个 style 属性:

<Button style="@style/BlueButton" ... />

这些对我来说似乎没有太大的不同。您可以像使用样式资源一样轻松地在 Java class 上使用 IDE 工具,例如 "find usages"。所以唯一的问题是你是否认为样式资源 "harder to manage" 比 Java class.

就我个人而言,我不认为他们是。

也许您不喜欢包含 500 个样式定义的巨大 styles.xml 文件。如果是这种情况,那么我会说正确的方法是将 styles.xml 文件拆分成更小的文件,而不是创建 Java classes。毕竟,您可以有一个 bluebutton.xml 文件,其中只有一个样式资源。


编辑:我将它放在下面,因为我认为它更重要。太糟糕了,我第一次没有想到它。

至于缺点,像您的 BlueButton 创建视图子 class 将 "break" 您在构造函数中设置的属性。假设我想使用 BlueButton,但这次我想要黑色文本而不是白色文本。其他都很"blue-button-ish",就是写不出来这个:

<app.ui.widget.BlueButton
    android:textColor="@android:color/black"
    ... />

在这种情况下,我的按钮仍然会有白色文本!那是因为 super 构造函数解析属性,然后 BlueButton 构造函数覆盖它们。另一方面,如果我使用了一种样式:

<Button
    android:textColor="@android:color/black"
    style="@style/BlueButton"
    ... />

我的按钮现在会有黑色文本。


我反对创建 View subclasses 而不是使用样式的另一个论点是 View subclass 无法强制实例在整个生命周期内匹配给定的样式应用程序。没有什么能阻止我向我的布局添加 BlueButton,然后稍后调用 button.setBackgroundResource(R.drawable.foo)

使用样式可以更明确地表明这只是按钮的 初始 状态。然后,我绝不会尝试通过 if (button instanceof BlueButton) 之类的操作来检查我的按钮是否具有蓝色背景,这甚至不能保证工作(如上所述)。

tl;dr:使 View subclasses 并不可怕,但我认为它比使用样式属性稍微差一点。