自分のコードからレイアウトの背景色を探したいと思います。それを見つける方法はありますか? linearLayout.getBackgroundColor()
のようなもの?レイアウトの背景色を取得する
答えて
これは、背景が単色である場合にのみAPI 11以降で達成できます。
int color = Color.TRANSPARENT;
Drawable background = view.getBackground();
if (background instanceof ColorDrawable)
color = ((ColorDrawable) background).getColor();
私はちょうど私の答えを編集し、具体的にはうまくいくと言いました!しかし、なぜAPI 11以上の制限があるのかわかりません。 'ColorDrawable'はAPI1とview.getBackground()から利用できるようです。 –
心配しないでください。私はColorDrawableの '.getColor'がAPI 11で追加されたことを知ります。 –
' Drawable'を 'Bitmap'に変換して最初のピクセルを得ることができます。 'int color = bitmap.getPixel(0、0);' –
ColorDrawable.getColorは()だけで11以上のAPIレベルで動作しますので、あなたは、APIレベルからAPIレベル以下の1.の反射を、それをサポートするために、このコードを使用することができます11.
public static int getBackgroundColor(View view) {
Drawable drawable = view.getBackground();
if (drawable instanceof ColorDrawable) {
ColorDrawable colorDrawable = (ColorDrawable) drawable;
if (Build.VERSION.SDK_INT >= 11) {
return colorDrawable.getColor();
}
try {
Field field = colorDrawable.getClass().getDeclaredField("mState");
field.setAccessible(true);
Object object = field.get(colorDrawable);
field = object.getClass().getDeclaredField("mUseColor");
field.setAccessible(true);
return field.getInt(object);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return 0;
}
へレイアウトの背景色を取得する:
LinearLayout lay = (LinearLayout) findViewById(R.id.lay1);
ColorDrawable viewColor = (ColorDrawable) lay.getBackground();
int colorId = viewColor.getColor();
RelativeLayoutの場合、そのIDを見つけ、LinearLayoutの代わりにオブジェクトを使用します。
これを実行する最も簡単な方法は次のとおりです。
view.getSolidColor();
ショートとシンプルな方法:
int color = ((ColorDrawable)view.getBackground()).getColor();
- 1. ウィンドウの背景色を取得する
- 2. Androidレイアウトの背景色の変更
- 3. JButtonのデフォルトの背景色を取得するには?
- 4. androidのボタンの背景色を取得する
- 5. Macのオンスクリーンウィンドウの背景色を取得するには
- 6. スイングコンポーネントのデフォルトの背景色を取得する
- 7. アンドロイドのボタンの背景色を取得する方法は?
- 8. ペインの背景色を取得するjavafx
- 9. ラベルの背景色を取得する方法 - iOS 4?
- 10. Javaのレイアウトの背景色を変更する
- 11. QPushButtonを取得する背景色を押した
- 12. Android:レイアウト外の背景色を変更する
- 13. JavaScriptが変数に背景色を取得する
- 14. xmlレイアウトで背景色を設定する方法は?
- 15. アンドロイドでトーストの背景色、背景色、テキスト色をカスタマイズする方法
- 16. #000形式の背景色を取得し、このコードで
- 17. Androidの背景でピクセルの色を取得
- 18. UIWebView htmlから背景色を取得していますか?
- 19. 背景グラデーションの色と背景イメージDRYly
- 20. CSS:背景色の背景画像
- 21. NSImageView +背景色
- 22. AlertDialog背景色
- 23. 背景色:Android
- 24. UITableViewCell背景色?
- 25. NILauncherView背景色
- 26. PreferenceFragment背景色
- 27. gnuplotの背景色
- 28. アンドロイドプリファレンスカテゴリの背景色
- 29. イオンタブの背景色
- 30. UIWebViewの背景色
背景は色ではないかもしれないので、あなたはlinearLayout.getBackgroundを(使用できる)を得れますあなたは 'Drawable'です。特に背景色を取得するAPIはありません。 [Viewのドキュメントをもっと読む](http://developer.android.com/reference/android/view/View.html#getBackground%28%29) –
しかし、私は本当にレイアウトの色を見つける必要があります。別の方法があるはずです!またはDrawableから取得することは可能ですか? –