您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
84_应用程序和activity的主题(theme)
发布时间:2021-03-26 15:16:22编辑:雪饮阅读()
弹窗式主题
在activity中默认的主题是继承自application的,但是一个activity也可以自定义自己的主题。比如下面这个清单文件中MainActivity继承子application的android:theme="@style/Theme.Theme
而MainActivity2则单独设置了主题android:theme="@android:style/Theme.Dialog"即弹窗式主题。
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.theme">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Theme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2" android:theme="@android:style/Theme.Dialog"></activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.theme">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Theme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2" android:theme="@android:style/Theme.Dialog"></activity>
</application>
</manifest>
为了能看出效果,在主布局文件中添加一个按钮,以意图的形式打开第二个activity
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="openActivity"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
对于第二个activity可以没有布局文件,但是其逻辑MainActivity2.java还是必须有的
package com.example.theme;
import android.app.Activity;
public class MainActivity2 extends Activity {}
那么当项目部署到设备后并点击主布局文件中按钮的那一刻
无标题栏的全屏主题
当上面的第二个activity使用了主题Theme.NoTitleBar.Fullscreen,就可以实现无标题栏的全屏主题了。
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.theme">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Theme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"></activity>
</application>
</manifest>
这里只修改了清单文件,其它照旧,同样部署到设备中并打开第二个activity如
利用style继承特性完成自定义theme
上面使用的主题都是系统内置的一些主题,其实也可以像之前style一样继承某个主题,然后完成一个自定义的主题,然后在清单文件中引入这个自定义主题。
strings.xml如:
<resources>
<string name="app_name">theme</string>
<style name="mytheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:background">@color/red</item>
</style>
<color name="red">#FFFF0000</color>
</resources>
然后清单文件中直接引入如AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.theme">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Theme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2" android:theme="@style/mytheme"></activity>
</application>
</manifest>
然后同样的,效果如
红红…
application级别的主题
一般的application级别的主题可以使得整个项目所以activity都默认使用这个主题
那么清单文件修改如AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.theme">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/mytheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2"></activity>
</application>
</manifest>
这里两个activity都没有显式的声明自己的主题了,只是在application中声明引用了一个自定义的主题
那么这个应用到application级别的自定义主题就不能像是上面的style一样随便继承了,这个继承关系需要调整下,则自定义style修改如strings.xml:
<resources>
<string name="app_name">theme</string>
<!--
如果一个style要做为给application级别引用时候,则该style中继承必须是
@style/Theme.AppCompat,@style/Theme.AppCompat.Light...之类,即Theme.AppCompat的后代才可以
像是之前继承的@android:style/Theme.NoTitleBar.Fullscreen就会在日志猫中有错误产生,并且项目启动不了
-->
<style name="mytheme" parent="@style/Theme.AppCompat">
<item name="android:background">@color/red</item>
</style>
<color name="red">#FFFF0000</color>
</resources>
最后整个项目所有activity都变成了红红了。。。
关键字词:android,activity,theme,主题