Android主题不适用于Activity

rqmkfv5c  于 2024-01-04  发布在  Android
关注(0)|答案(2)|浏览(273)

当我在布局中设置背景颜色时,一切都很完美。然而,**当我试图从主题设置它时,它不起作用。**似乎我做错了什么。下面是代码。

清单:

  1. <activity android:name="com.example.somma.MainActivity"
  2. android:label="@string/app_name"
  3. android:theme="@style/custom_title_theme"
  4. >
  5. <intent-filter>
  6. <action android:name="android.intent.action.MAIN" />
  7. <category android:name="android.intent.category.LAUNCHER" />
  8. </intent-filter>
  9. </activity>

字符串

custom_title_theme.xml

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


这是因为在每一个类中,我都调用这些行onStart()吗?

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

custom_title_bar.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@drawable/custom_title_background">
  6. <TextView android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:textSize="16sp"
  9. android:textColor="@color/cream_brown"
  10. android:textStyle="bold"
  11. android:id="@+id/custom_title_text"
  12. android:text="@string/app_name"
  13. android:layout_centerInParent="true"
  14. android:shadowColor="@android:color/black"
  15. android:shadowRadius="3"/>
  16. <Button android:id="@+id/settings_button"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_alignParentRight="true"
  20. android:text="@string/settings"
  21. android:onClick="openSharedPref"
  22. android:background="@drawable/header_button"/>
  23. </RelativeLayout>


getWindow(). setObjectureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_bar);是否阻止设置“custom_title_theme”?
这里是一个屏幕上的一切是如何出现的:
x1c 0d1x的数据

cwtwac6a

cwtwac6a1#

我建议你尝试以下操作:

  1. <application
  2. android:icon="@drawable/icon"
  3. android:label="@string/app_name"
  4. android:theme="@style/custom_title_theme"/>

字符串

gmxoilav

gmxoilav2#

添加主题到你的android manifest文件。
您缺少@android:style。
使用

  1. android:theme="@android:style/custom_title_theme" instead of
  2. android:theme="@style/custom_title_theme"

字符串

相关问题