【问题标题】:Android custom style/scheme is not applied correctly to button-textColorAndroid 自定义样式/方案未正确应用于 button-textColor
【发布时间】:2012-09-26 03:45:25
【问题描述】:

我无法将自定义方案(样式)应用于某些视图。我在 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>

我已将此样式应用于我的简单测试应用程序的主要活动,如下所示

   <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>

MainActivity 的布局包含几个按钮,一些 textView,其中一些明确指定了 style 属性,其他应该从整个 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>

据我所知,活动中的所有内容都应该根据我定义的样式进行风格化,但是,这就是我得到的

如您所见,只有具有指定样式的按钮的文本为红色,其他两个没有。然而,即使是白色按钮也能从该方案中获得正确的字体,因此至少有一部分正在被应用。为什么不是全部?任何帮助将不胜感激,我非常困惑。

【问题讨论】:

    标签: android android-layout button android-styles


    【解决方案1】:

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

    <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 manifest 中你应该设置主题...

    <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>
    

    【讨论】:

    • 这不会以任何方式更改按钮。更奇怪的是,它并没有改变任何东西。 textEdits 仍然保持紫色,尽管我希望这个 AppTheme 只定义按钮的样式,其余的应该是默认的。
    • 您的 MainActivity 是否对按钮进行任何图形更改?那会覆盖主题。
    • 不,setContentView(R.layout.activity_main);在 onCreate() 中几乎是活动中唯一发生的事情
    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2012-06-20
    • 2013-01-22
    • 2012-03-07
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    相关资源
    最近更新 更多