2016-12-11 1 views
2

私は、次のコードを使用しています:どのようにすべての活動の永久的な背景を設定するには?

Public void xyz(View v) { 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode){ 
     case 0: 
      data.getDataString(); 
      if(resultCode == RESULT_OK){ 
       try { 
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(_activity.getContentResolver(), data.getDataString()); 
        RelativeLayout bg = (RelativeLayout) findViewById(R.id.might); 
        Drawable drawable = new BitmapDrawable(getResources(), bitmap); 
        bg.setBackgroundDrawable(drawable); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch blocke.printStackTrace(); 
       } catch(IOException e) { 
        // TODO Auto-generated catch blocke.printStackTrace(); 
       } 
      }     
      break; 
    } 
} 

質問:

  1. それが唯一の活動の背景を設定します。

  2. onDestroy()メソッドの後、再起動時にデフォルトの背景を設定します。

+3

コードの書式を学ぶ:http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks –

+0

あなたはすべてのスーパークラスであるアクティビティクラスを書くことができますあなたの他のアクティビティ –

+1

選択したイメージのURLをシングルトンクラス/ SharedPreferencesに保存し、アクティビティが作成されたときに読み込むことができます。おそらくPicassoのようなライブラリのキャッシュを使って、画像がメモリに残っていれば読み込まないようにすることもできます。 –

答えて

1

は(onactivityresultに入れて)

FileOutputStream outputStream = null; 
    try { 
outputStream = openFileOutput("filename.jpg", Context.MODE_PRIVATE); 
// Use the compress method on the BitMap object to write image to the OutputStream 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } finally { 
         try { 
          outputStream.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 

//ロードイメージを内部ストレージに画像を保存して、

try { 
     FileInputStream l = openFileInput("filename.jpg"); 
     Bitmap A = BitmapFactory.decodeStream(l); 
     LinearLayout bg = (LinearLayout) findViewById(R.id.layoutid); 
     Drawable drawable = new BitmapDrawable(getResources(), A); 
     bg.setBackgroundDrawable(drawable); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

おかげマルチンJedynak背景(onsatrtactivity)に設定します!

+0

これをコーディングしてくれてありがとう!! –

2

1つのMainActivityを使用できます。次に、フラグメントを画面に使用します。 MainActivityのレイアウトにあなたの色や背景drawableを与えます。しかし、全てのフラグメントのレイアウトに次の操作を行います

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#00000000" 

第2 00年代には、背景を透明にするためのものです。

0

Rupinderjeet asnwerが正しいです。あなたはデフォルトの背景で作業することができます。

ただし、は、これを行うための簡単な方法です。

はあなたがここにもcolorbitmapsまたはdrawablesを置くことができ、あなたのアプリのテーマ

によって変更Base.Theme.AppCompat.Lightvalues

<style name="DefaultBackgroundTheme" parent="Base.Theme.AppCompat.Light"> 
    <item name="android:windowBackground">@drawable/splash_bitmap</item> 
</style> 

に配置されたあなたのstyle.xmlでスタイルを作ります。

とマニフェストクラス内

<activity 
    android:name="yourActivityName" 
    android:theme="@style/DefaultBackgroundTheme" /> 

であなたのテーマを変更するには、これはあなたのお役に立てば幸いです。

関連する問題