Android:Theme的使用-全屏设置

博客围绕Android中Theme.AppCompat相关问题展开,提到遇到如找不到‘@style/Theme.AppCompat.Light’资源等问题,分析原因是activity继承了兼容包中的类,需使用AppCompat的theme,还给出了设置全屏在AndroidMainfest.xml标签的配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.cnblogs.com/zhouyou96/p/5323138.html
https://www.jianshu.com/p/c797f90c4168
https://blog.youkuaiyun.com/howard2005/article/details/79460799
https://blog.youkuaiyun.com/u013694478/article/details/79468952

Android关于Theme.AppCompat相关问题的深入分析
No resource found that matches the given name ‘@style/Theme.AppCompat.Light’
AppTheme相关

遇到了一个问题:

想设置一个全屏,在AndroidMainfest.xml的标签的配置如下:

        <activity
            android:name=".activity.ManagerActivity"
            android:screenOrientation="landscape"
            android:theme="@style/AppTheme" />```

values/style

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.DeviceDefault.Light.NoActionBar.Fullscreen">
        <item name="android:windowNoTitle">true</item>
    </style>

原因:

从错误提示中提到Theme.AppCompat theme,因为我们的activity一定是继承了兼容包中的类(我这里继承AppCompatActivity),
所以就要使用与其配合的AppCompat的theme才行。

解决办法:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowFullscreen">true</item>
    </style>

下面是错误提示:

2018-01-01 15:37:45.994 19821-19821/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.snap.awesomeserial, PID: 19821
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snap.awesomeserial/com.snap.awesomeserial.ui.DemoActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2805)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2883)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)
        at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.snap.awesomeserial.base.BaseActivity.onCreate(BaseActivity.java:17)
        at com.snap.awesomeserial.ui.DemoActivity.onCreate(DemoActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:7023)
        at android.app.Activity.performCreate(Activity.java:7014)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2758)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2883) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6523) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857) 
### 如何在 Android 中修改 `android:theme` 属性以更改应用程序主题 #### 修改全局应用主题 为了在整个应用程序中设置统一的主题,在项目的根级 `AndroidManifest.xml` 文件中的 `<application>` 标签里指定 `android:theme` 属性[^1]。 ```xml <application android:theme="@style/Theme.AppCompat" ...> </application> ``` 这会将整个应用程序的主题设定为 `Theme.AppCompat`,从而影响所有活动 (Activity) 和视图组件的外观样式。 #### 更改单个 Activity 的主题 对于特定的 Activity 可单独配置其主题。例如要使某个 Activity 显示成对话框形式,则可以在该 Activity 对应的声明处加入如下代码片段[^2]: ```xml <activity android:name=".YourDialogActivity" android:theme="@android:style/Theme.Dialog"> </activity> ``` 这样当启动这个名为 `.YourDialogActivity` 的界面时就会呈现为悬浮窗效果而非全屏展示。 #### 解决因更换主题导致的应用程序崩溃问题 有时替换默认主题可能会引发兼容性错误进而造成闪退现象发生。遇到这种情况建议先确认所选的新主题是否完全适配当前使用的 SDK 版本以及依赖库版本;另外还需注意检查布局文件和其他资源文件是否存在与新主题冲突的地方[^3]。 #### 动态调整主题颜色方案 除了静态地定义好固定不变的一套主题外,还可以利用自定义属性来实现动态切换不同色调的效果。比如借助于反射机制操作某些控件内部状态的方法之一就是重设边缘发光特效的颜色值[^4]: ```java import androidx.core.view.ViewCompat; //... public static void setEdgeGlowColor(@NonNull ViewPager viewPager, @ColorInt int color){ try { Field mLeftEdgeField = ViewPager.class.getDeclaredField("mLeftEdge"); Field mRightEdgeField = ViewPager.class.getDeclaredField("mRightEdge"); mLeftEdgeField.setAccessible(true); EdgeEffect leftEdge = (EdgeEffect)mLeftEdgeField.get(viewPager); mRightEdgeField.setAccessible(true); EdgeEffect rightEdge = (EdgeEffect)mRightEdgeField.get(viewPager); ViewCompat.setBackgroundTintList(leftEdge,new ColorStateList(new int[][]{new int[]{}}, new int[]{color})); ViewCompat.setBackgroundTintList(rightEdge,new ColorStateList(new int[][]{new int[]{}}, new int[]{color})); } catch (Exception e) { Log.e(TAG,"Failed to change edge glow color",e); } } ``` 此函数允许开发者传入任意合法的颜色整数值作为参数传递给目标 `ViewPager` 实例对象,以此达到即时更新滚动条两侧光晕色彩的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值