在TextView/EditText上使用"android:textAppearance"失败,但"样式"有效

Seb*_*oth 18 android

这是XML中的TextView示例,它将样式正确应用于TextContactContactNameTextView.

<TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/photo"
        android:layout_alignTop="@id/photo"
        android:paddingLeft="10dp"
        android:text="First Last"
        style="@style/TextContactContactName"
        />
Run Code Online (Sandbox Code Playgroud)

现在,我还要在此元素上设置其他一些内容,这些内容与文本无关.因此我尝试移动TextContactContactName并通过"android:textAppearance"应用它

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/photo"
    android:layout_alignTop="@id/photo"
    android:paddingLeft="10dp"
    android:text="First Last"
    android:textAppearance=@style/TextContactContactName"
    />
Run Code Online (Sandbox Code Playgroud)

不幸的是,没有应用文本格式.

样式(存储在res/values/styles-text.xml)中的样式如下:

<style name="TextContactContactName">
    <item name="android:textSize">24sp</item>
    <item name="android:textColor">#333333</item>
    <item name="android:shadowColor">#ffffff</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">1</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我使用textAppearance标签错了吗?

我测试了Android 2.2 - HTC Desire.

Seb*_*oth 22

好吧,看起来像我的错.我做了一个快速测试:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text styled using 'style'."
        style="@style/UiTestTextView"
        />
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text styled using 'textAppearance'."
        android:textAppearance="@style/UiTestTextView"
        />
Run Code Online (Sandbox Code Playgroud)

和styles-text.xml:

<style name="UiTestTextView">
    <item name="android:textColor">#ff0000</item>
    <item name="android:textSize">16sp</item>

    <item name="android:shadowColor">#ffffff</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">2</item>
    <item name="android:shadowRadius">3</item>
</style>
Run Code Online (Sandbox Code Playgroud)

关键是要记住shadow*元素不是文本属性.因此不会通过textAppearance应用.

我将在Android Developers Mailing列表中询问这是否是所需的行为.

  • 你有没有得到开发者的答复?我也很高兴也知道. (3认同)