ボタンの背景色を取得する方法を教えてください。 xmlでは、---- android:background = XXXXX アクティビティクラス内の背景色を設定しました。この値はどのように取得できますか?androidのボタンの背景色を取得する
33
A
答えて
73
残念ながら、実際の色を取得する方法はわかりません。
それはあなたが、これは、あなたが試すことができ
ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
色であることがわかっている場合Drawable
Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();
としてこれを取得するのは簡単ですそして、あなたは、Androidにしている場合は、抜け出すことができ3.0+色のリソースID
int colorId = buttonColor.getColor();
これをあなたの割り当てられた色と比較してください。
if (colorID == R.color.green) {
log("color is green");
}
2
背景Drawable
を取得するには基本View
クラスで定義されている、あなたは
public Drawable getBackground();
を使用しています。
Button
は、背景、画像、色、グラデーションを持つことができます。あなたがアンドロイドを使用する場合:背景= "#FFFFFFのを"、背景のクラスが
android.graphics.drawable.ColorDrawableが
そこから、あなたは、単に呼び出すことができるでしょう
public int getColor()
14
また、取得するための最も単純な方法
android:tag="#ff0000"
とコード
String colorCode = (String)btn.getTag();
19
private Bitmap mBitmap;
private Canvas mCanvas;
private Rect mBounds;
public void initIfNeeded() {
if(mBitmap == null) {
mBitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mBounds = new Rect();
}
}
public int getBackgroundColor(View view) {
// The actual color, not the id.
int color = Color.BLACK;
if(view.getBackground() instanceof ColorDrawable) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
initIfNeeded();
// If the ColorDrawable makes use of its bounds in the draw method,
// we may not be able to get the color we want. This is not the usual
// case before Ice Cream Sandwich (4.0.1 r1).
// Yet, we change the bounds temporarily, just to be sure that we are
// successful.
ColorDrawable colorDrawable = (ColorDrawable)view.getBackground();
mBounds.set(colorDrawable.getBounds()); // Save the original bounds.
colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds.
colorDrawable.draw(mCanvas);
color = mBitmap.getPixel(0, 0);
colorDrawable.setBounds(mBounds); // Restore the original bounds.
}
else {
color = ((ColorDrawable)view.getBackground()).getColor();
}
}
return color;
}
+3
これは受け入れられた答えでなければなりません – IHeartAndroid
5
からアクセスするようなタグとして色の値を設定するような何かを試すことができます私の色は:
int color = ((ColorDrawable)button.getBackground()).getColor();
ですテスト済み
、これを試してみてくださいロリポップ5.1.1
0
に取り組んで:
list_view.getChildAt(position).setBackgroundColor(Color.YELLOW);
ColorDrawable corItem = (ColorDrawable) list_view.getChildAt(position).getBackground();
if(corItem.getColor() == Color.YELLOW){
Toast.makeText(NovoProcessoActivity.this,"Right Color!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(NovoProcessoActivity.this,"Wrong Color!", Toast.LENGTH_SHORT).show();
}
または
int color =((ColorDrawable) list_view.getChildAt(position).getBackground()).getColor();
関連する問題
- 1. アンドロイドのボタンの背景色を取得する方法は?
- 2. JavaScriptで「背景色:背景」 - 色を取得するには?
- 3. ttkボタンの背景色を取得するには?
- 4. ウィンドウの背景色を取得する
- 5. レイアウトの背景色を取得する
- 6. 背景色:Android
- 7. webpartのボタンの背景色
- 8. ボタンの背景色save java
- 9. デフォルトの背景色を取得
- 10. テキストボックスの背景色を取得
- 11. Androidの背景でピクセルの色を取得
- 12. ボタンの背景色を変更する
- 13. 背景色が設定されていないと、Androidは親ビューの背景色を取得します
- 14. Androidトグルボタン - 背景色
- 15. Androidアプリケーションの背景色
- 16. 背景色を変更するボタン
- 17. 変更ボタンの背景色を
- 18. Android:背景色を変更せずに丸いボタンのコーナー
- 19. 現在のQuillエディタのツールバーの背景色を取得する
- 20. Javaの変更ボタンの背景色
- 21. 2つの背景色のCSSボタン
- 22. C#UWPのボタンの背景色
- 23. android xmlからAndroidボタンの背景色(normal + pressed)を設定する方法
- 24. スイングコンポーネントのデフォルトの背景色を取得する
- 25. Macのオンスクリーンウィンドウの背景色を取得するには
- 26. JButtonのデフォルトの背景色を取得するには?
- 27. Android - SimpleCursorAdapter.ViewBinder - 背景色を設定する
- 28. ボタンの背景色が煎茶
- 29. CheckBoxの背景色を取得する方法
- 30. ラベルの背景色を取得する方法 - iOS 4?
あなたはGETCOLOR()は、IDを取得しますか?私はそれが色の実際のint値を取得すると思う(すなわち0xAARRGGBB)。私はこれを "#00000001"でテストし、1を返しました。 – brianestey
私のテストでは、 'button.setBackground(R.color.green)'を実行し、応答をチェックしました。私は 'Color.red(int)'、 'Color.green(int)'、 'Color.blue(int)'とすることができるように、適切な色整数を好むでしょう。しかし、Android 3.2での私のテストでは、そうではありませんでした。私はそれが矛盾していると推測し、文脈に応じて、色int、または残余を返します。 –
それはAPI 11+ –