Android中读取另一个apk中的资源

在Android系统中,apk之间可以相互读取数据的条件是:有同样的签名,并且AndroidManifest.xml文件中配置的android:sharedUserId属性值相同,那么两个apk运行在同一个进程中,可以互相访问任意数据。

方法如下:

1、主程序及要读取的apk中AndroidManifest.xml中配置

例如: android:sharedUserId="com.salc3k"

2、访问资源的方法

@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
 
Context slaveContext = null ;
 
try {
slaveContext = this .createPackageContext(
"com.salc3k.slave" , Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
 
Button btn = (Button) findViewById(R.id.btn);
btn.setBackgroundDrawable(slaveContext.getResources().getDrawable(
R.drawable.btn_background));
}

这一功能用来为程序制作皮肤。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用ImageView控件和Button控件来实现二个按钮控制图片浏览。具体实现步骤如下: 1. 在布局文件添加一个ImageView控件和两个Button控件,分别表示上一张和下一张图片的按钮。 ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter"/> <Button android:id="@+id/previousButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Previous" android:layout_alignParentLeft="true" android:layout_centerVertical="true"/> <Button android:id="@+id/nextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" android:layout_alignParentRight="true" android:layout_centerVertical="true"/> </RelativeLayout> ``` 2. 在Activity获取ImageView和Button控件的引用,并为Button控件添加点击事件。 ``` public class MainActivity extends AppCompatActivity { private ImageView imageView; private Button previousButton; private Button nextButton; private int[] imageIds = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4}; private int currentImageIndex = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); previousButton = findViewById(R.id.previousButton); nextButton = findViewById(R.id.nextButton); previousButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (currentImageIndex > 0) { currentImageIndex--; imageView.setImageResource(imageIds[currentImageIndex]); } } }); nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (currentImageIndex < imageIds.length - 1) { currentImageIndex++; imageView.setImageResource(imageIds[currentImageIndex]); } } }); imageView.setImageResource(imageIds[currentImageIndex]); } } ``` 3. 在Activity定义一个整型数组,存储需要显示的图片资源ID。并定义一个整型变量,表示当前显示的图片索引。 4. 在Button的点击事件,根据当前显示的图片索引和按钮的类型(上一张或下一张),更新当前显示的图片索引,并将ImageView的图片资源设置为对应的图片资源ID。 5. 在Activity的onCreate方法,初始化ImageView和Button控件,并将第一张图片设置为初始显示的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值