Android::如何以编程方式从其他apk更改主题
我有一个名为:preference的应用程序,我想为此应用程序制作主题。我首选的AndroidManifest.xml是:Android::如何以编程方式从其他apk更改主题,android,themes,Android,Themes,我有一个名为:preference的应用程序,我想为此应用程序制作主题。我首选的AndroidManifest.xml是: <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity
<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>
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;
}
}
我有一个主题应用程序,它有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>
我已收到主题包的主题对象,并尝试设置首选项应用程序的主题。但它不起作用。请帮助我:如何以编程方式将主题从其他应用程序设置为当前应用程序 如果您想从一个应用程序更改另一个应用程序中的设置,那么最简单的方法可能是将主题ID放入意图中,并将该意图发送到您的MainActivity(或IntentService),接收应用程序可以在其中处理数据。例如,当意图到达时,您可以像在“onClick”逻辑中执行单击事件一样对其进行处理 例如,应用程序可以创建如下意图:
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);
然后在您的活动中,使用
getIntent().getStringExtra(“主题id”)
获取传递到应用程序的数据。您需要使用应用程序的资源来加载主题。我在这里的另一个帖子中回答了这个问题:
下面是一个安装了包“com.example.theme”的示例,我们在另一个应用中使用该应用的资源和主题样式:
public class MainActivity extends AppCompatActivity {
Resources resources;
@Override protected void onCreate(Bundle savedInstanceState) {
int themeResId = getResources().getIdentifier("AppTheme", "style", "com.example.theme");
if (themeResId != 0) {
setTheme(themeResId);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override public Resources getResources() {
if (resources == null) {
try {
resources = getPackageManager().getResourcesForApplication("com.example.theme");
} catch (PackageManager.NameNotFoundException e) {
resources = super.getResources();
}
}
return resources;
}
}
有关GitHub上的工作项目,请参阅
这可能会导致问题,因为所有布局、绘图、字符串等都将从其他应用程序的资源中加载。因此,您应该避免使用另一个包中的主题,而是将资源和主题复制到您自己的项目中
public class MainActivity extends AppCompatActivity {
Resources resources;
@Override protected void onCreate(Bundle savedInstanceState) {
int themeResId = getResources().getIdentifier("AppTheme", "style", "com.example.theme");
if (themeResId != 0) {
setTheme(themeResId);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override public Resources getResources() {
if (resources == null) {
try {
resources = getPackageManager().getResourcesForApplication("com.example.theme");
} catch (PackageManager.NameNotFoundException e) {
resources = super.getResources();
}
}
return resources;
}
}