gpt4 book ai didi

Android::如何以编程方式更改其他 apk 的主题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:01 26 4
gpt4 key购买 nike

我有一个名为:preference 的应用程序,我想为这个应用程序制作主题。我偏好的 AndroidManifest.xml 是:

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.preference.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

在 MainActivity.java 中:我收到主题更改按钮点击:

themeChange = (Button) findViewById(R.id.themeChange);
themeChange.setOnClickListener(this);

我有一个主题应用程序,它有 PinkTheme (styles.xml),包名称是 com.example.theme。

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<style name="PinkTheme" parent="AppBaseTheme" >
<item name="android:textColor">#FF69B4</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">40sp</item>
<item name="android:windowBackground">#008000</item>
</style>
</resources>

我喜欢的 onClick() 是:

public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){

case R.id.themeChange:
Resources res = null;
try {
res = getPackageManager().getResourcesForApplication("com.example.theme");
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(null != res) {
int sId = res.getIdentifier("com.example.theme:style/PinkTheme", null, null);
Theme themeObject = res.newTheme(); // theme object
themeObject.applyStyle(sId, true); // Place new attribute values into the theme
getTheme().setTo(themeObject);
}
break;

default:
break;

}
}

收到主题包的theme对象,尝试设置偏好应用的主题。但它不起作用。请帮助我:如何以编程方式将其他应用程序的主题设置为我当前的应用程序。

最佳答案

如果您想更改一个应用程序中另一个应用程序的设置,那么最简单的方法可能是将主题 ID 放入 Intent 并将该 Intent 发送到接收应用程序可以接收的 MainActivity(或 IntentService)处理数据。例如,当 Intent 到达时,您可以像在“onClick”逻辑中处理点击事件一样处理它。

例如,应用可以创建这样的 Intent :

    Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(new ComponentName("your.package", "your.package.component"));
intent.putExtra("theme_id", "theme_1");
startActivity(intent);

然后在您的 Activity 中,使用 getIntent().getStringExtra("theme_id") 获取传递给您的应用的数据。

关于Android::如何以编程方式更改其他 apk 的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25226294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com