Android theme won't apply to activity

て烟熏妆下的殇ゞ 提交于 2019-12-11 07:46:24

问题


When I set the background color in the layout everything works perfectly. However, when I try to set the it from a theme it won't work. It seems I'm doing something wrong. Here is the code.

Manifest:

    <activity android:name="com.example.somma.MainActivity"
    android:label="@string/app_name"
    android:theme="@style/custom_title_theme"
    >
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity> 

custom_title_theme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_title_theme" parent="android:Theme">
    <item name="android:windowTitleSize">40dp</item>
    <item name="android:windowTitleBackgroundStyle">@drawable/custom_title_background</item>
    <item name="android:background">@color/Meerenguee</item>
</style>    
</resources>

Is that because in every class I call these lines onStart()?

      requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
      setContentView(com.example.somma.R.layout.sumwindow);
      getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);

custom_title_bar.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@drawable/custom_title_background">

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="16sp"
          android:textColor="@color/cream_brown"
          android:textStyle="bold"
          android:id="@+id/custom_title_text"
          android:text="@string/app_name"
          android:layout_centerInParent="true"
          android:shadowColor="@android:color/black"
          android:shadowRadius="3"/>

<Button android:id="@+id/settings_button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentRight="true"
android:text="@string/settings"
android:onClick="openSharedPref"
android:background="@drawable/header_button"/>

</RelativeLayout>

Does getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar); prevents "custom_title_theme" from being set?

Here is a screen of how everything appears:


回答1:


I suggest you try following:

<application 
   android:icon="@drawable/icon" 
   android:label="@string/app_name" 
   android:theme="@style/custom_title_theme"/>



回答2:


Add theme into your android manifeast file.

Your are missing @android:style.

use

android:theme="@android:style/custom_title_theme" instead of 
android:theme="@style/custom_title_theme"


来源:https://stackoverflow.com/questions/18445550/android-theme-wont-apply-to-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!