2012-08-16 67 views
1

我想找到一種方法將背景資源應用於多個按鈕。這些資源來自SDCARD。應用資源的通常方式是這樣的:應用SDCARD的背景資源

Button b = new Button(getContext()); 
b.setBackgroundResource(R.drawable.button_states); 
//where button_states is an XML file 

但我不知道如何應用背景,如果XML文件是從SD卡。有任何想法嗎?

+0

你在SD卡中存儲的XML文件,並試圖使用它? – 2012-08-16 04:17:45

+0

是沙拉斯G,我存儲XML在SD卡 – 2012-08-16 04:25:08

+0

我可以知道需要存儲在XML文件中的SD卡@Jayson塔馬約 – 2012-08-16 04:35:34

回答

2

使用下面的代碼對於這一點,它可以幫助你。

Button b = new Button(getContext()); 
Bitmap bmp = BitmapFactory.decodeFile("/mnt/sdcard/test.png"); 
Drawable d = new BitmapDrawable(bmp); 
b.setBackgroundDrawable(d); 
1
String rootPath =Environment.getExternalStorageDirectory().getAbsolutePath(); // Returns path to sdcard 
rootPath+="/test.png" 
Bitmap b = BitmapFactory.decodeFile(s); 
mImageView.setImageBitmap(b); 
0

而不是存儲在SD卡使用這樣的事情

<selector> 
<item> 
    <shape> 
     <gradient 
      android:startColor="#343434" 
      android:endColor="#171717" 
      android:angle="270" /> 
     <stroke 
      android:width="1dp" 
      android:color="#171717" /> 
     <corners 
      android:radius="4dp" /> 
     <padding 
      android:left="10dp" 
      android:top="10dp" 
      android:right="10dp" 
      android:bottom="10dp" /> 
    </shape> 
</item> 
</selector> 

XML文件,請嘗試使用此作爲繪製文件夾中一個單獨的XML文件,並將其應用到需要的按鈕,像

style="@style/ButtonText 

在佈局xml文件中