[英]ImageView black and white not working in alert dialog
我希望自定義警報對話框中的imageview為黑白。 正確的方法(經過搜索后)是...
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageview.setColorFilter(filter);
但是,當我的imageview是我的自定義警報對話框時,此方法不起作用。
這是我的代碼:
私人void showDialogForTodayItemClicked(){
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_daily_food_clicked);
dialog.setTitle("Title...");
ImageView imageview = (ImageView) dialog.findViewById(R.id.image_view_dialog_background_today);
imageview.setBackgroundResource(R.drawable.avocados_small);
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageview.setColorFilter(filter);
dialog.show();
}
這是對話框的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/dialog_daily_food_relative_layout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffffff"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#81383838"></LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image_view_dialog_background_today"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textColor="#ffffff"
android:layout_margin="20dp"
android:textStyle="bold"
android:text="MORE INFO"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:textStyle="bold"
android:layout_margin="20dp"/>
</RelativeLayout>
非常感謝大家。
答案是,當我使用它時它起作用了:
imageview.setImageResource(currentItem.getImage());
(而不是setBackgroundImage)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.