如何继承自定义 styles.xml 中的默认 Android 样式?

edw*_*ard 3 android textview android-layout android-edittext android-styles

我目前正在使用这个自定义样式布局来EditTexts定义“styles.xml”:

<style name="Form_EditView">
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">@dimen/large_text_size</item>
    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/pink</item>
    <item name="colorControlHighlight">@color/pink</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然而现在我陷入了困境,因为我TextView希望将样式设置为默认的 Android EditText,但随后也应用上面的自定义样式。默认的 Android 样式如下所示:

style="@android:style/Widget.EditText"
Run Code Online (Sandbox Code Playgroud)

我知道布局样式文件中可以继承,但如何在自定义样式中继承 Android 默认样式,以实现与此类似的效果:

<style name="Widget.EditText.Form_EditView">
Run Code Online (Sandbox Code Playgroud)

Pav*_*dka 5

继承其他样式的最简单方法是使用parent属性:

<style name="Form_EditView" parent="@android:style/Widget.EditText">
Run Code Online (Sandbox Code Playgroud)

正如 @Bobby StJacques 指出的,如果你想继承你定义的样式(不是内置的 Android 样式),你可以使用以下形式:

<style name="Form_EditView.SomeCustomizedEditView">
    ....
</style>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您继承了已经定义的Form_EditView

  • 事实上,如果您从另一个包中的样式继承,这是唯一的方法。如果它们位于同一个包中,您可以使用点表示法(这可以说更简单)。 (3认同)