我是android编程的新手。当显示时,我想在一个xml中动态设置图像。具体来说,我将项目相关图像存储在drawable文件夹中。另外,我将图像名称存储在字符串变量中,并且我正在尝试将这些图像设置为imageview。但图像没有显示。
我的代码:
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.stone );
imageView = ( ImageView )findViewById( R.id.stone_xxxx );
Intent intent = getIntent();
position = intent.getStringExtra( "POSITION" );
if ( position == "0" )
{
int imageResource = getResources().getIdentifier( "@drawable/stone_a1", null, getPackageName() );
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable( res );
}
}
答案 0 :(得分:1)
在ImageView中设置
iv.setImageResource(R.drawable.image)
答案 1 :(得分:0)
试试这个
final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
imageView.setBackgroundDrawable( getResources().getDrawable(R.drawable.stone_a1) );
} else {
imageView.setBackground( getResources().getDrawable(R.drawable.stone_a1));
}
答案 2 :(得分:0)
ImageView imageView = new ImageView(this);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
imageView.setImageBitmap(bmp);
答案 3 :(得分:0)
希望它对你有用..
imagev1=(ImageView) findViewById(R.id.imageViewslider22);
final int []imageArray={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.slider3,R.drawable.slider4,R.drawable.slider5,R.drawable.slider6,R.drawable.slider7};
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
imagev1.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 30000); //for interval...
}
};
handler.postDelayed(runnable, 2000); //for initial delay..