0

I'm having trouble with applying a custom scheme(style) to certain views. I have defined my own style in res/values/styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">


    <style name="myStyle1">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/red</item>
        <item name="android:textColorHighlight">@color/blue</item>
        <item name="android:textColorHint"> @color/fuchsia</item>
        <item name="android:textColorLink">@color/olive</item>
        <item name="android:typeface">serif</item>
    </style>

    <style name="AppTheme" parent="android:Theme.Light" />

</resources>

I have applied this style to the main activity of my simple test application, like this

   <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >


        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/myStyle1" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
           .
           .
           .
    </application>

The layout of the MainActivity contains a couple of buttons, some textViews, some of them have the style attribute spiecified explicitly, others should be inhertiting it from the whole activity.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/silver"
    android:orientation="vertical" >

    <EditText android:id="@+id/edit_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:hint="@string/edit_message" />

    <Button android:id="@+id/send_button"
        style="@style/myStyle1"
        android:text="@string/button_send" />

    <Button android:id="@+id/relative_activity"
        android:text="@string/rel_activitity_text"
        android:onClick="startRelativeActivity"/>

    <Button android:id="@+id/button_TTT"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:textColor="@color/red"
        android:text="@string/button_TTT" /> 

    <fragment android:name="my.firstapp.Fragment1"
            android:background="@color/red"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

</LinearLayout>

As far as I can tell, everything in the activity should be stylized according to the style I ahve defined, however, this is what I get

weird scheme

As you can see, only the buttons with the specified style have their text in red, the other two don't. However, even the white buttons get the correct font from the scheme, so at least some of it is being applied. Why not all of it? Any help would be appreciated, I'm thoroughly confused.

4

1 回答 1

0

如果您想要所有按钮的全局样式,您需要执行类似的操作。

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="myStyle1">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">@color/red</item>
    <item name="android:textColorHighlight">@color/blue</item>
    <item name="android:textColorHint"> @color/fuchsia</item>
    <item name="android:textColorLink">@color/olive</item>
    <item name="android:typeface">serif</item>
</style>

<style name="AppTheme" parent="android:Theme.Light">
    <item name="android:buttonStyle">@style/myStyle1</item>
</style>

</resources>

在您的android清单中,您应该设置主题...

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
       .
       .
       .
</application>

您可以对复选框等执行相同的操作。来自此链接的更多信息。 http://developer.android.com/reference/android/R.attr.html

我也在以下布局上对此进行了测试。所有 3 个按钮都应该有红色文本。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffaaaaaa"
    android:orientation="vertical" >

<EditText android:id="@+id/edit_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:hint="edit_message" />

<Button android:id="@+id/send_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="button_send" />

<Button android:id="@+id/relative_activity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="rel_activitity_text"
    android:onClick="startRelativeActivity"/>

<Button android:id="@+id/button_TTT"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:textColor="#ffff0000"
    android:text="button_TTT" /> 

<!--<fragment android:name="my.firstapp.Fragment1"
        android:background="#00ff0000"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />-->

</LinearLayout>
于 2012-09-25T17:13:58.453 回答