2016-04-25 197 views
0

我上點擊一個ImageView的。代碼創建一個警告對話框的大小下面給出:設置背景顏色改變按鈕

imgEditStatus.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       AlertDialog.Builder builder = new AlertDialog.Builder(ProfileActivity.this); 
       inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View dialogView = inflater.inflate(R.layout.dialog_edit_status, null); 
       builder.setView(dialogView); 
       dialog = builder.create(); 
       dialog.show(); 

       final EditText edtStatus = (EditText) dialogView.findViewById(R.id.editStatus); 

       Button btnOK = (Button) dialogView.findViewById(R.id.btnOK); 
       // btnOK.getBackground().setColorFilter(getResources().getColor(R.color.orange_color)), PorterDuff.Mode.SRC_ATOP); 
       btnOK.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         // Toast.makeText(getApplicationContext(), "OK clicked", Toast.LENGTH_SHORT).show(); 
         String statusText = edtStatus.getText().toString().trim(); 
         new UpdateStatusTask().execute(statusText); 
        } 
       }); 

       Button btnCancel = (Button) dialogView.findViewById(R.id.btnCancel); 
       btnCancel.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show(); 
         dialog.dismiss(); 
        } 
       }); 
      } 
     }); 

dialog_edit_status.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <EditText 
     android:id="@+id/editStatus" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/margin20" 
     android:hint="Enter your status here" 
     android:paddingBottom="@dimen/padding10" 
     android:padding="@dimen/padding10" 
     android:textColor="@color/black_color" 
     android:textColorHint="@color/gray_color" 
     android:layout_marginBottom="@dimen/margin10"/> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/margin10"> 

     <Button 
      android:id="@+id/btnOK" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="@dimen/margin50" 
      android:text="OK" 
      android:background="@color/orange_color" 
      android:textColor="@color/white_color"/> 

     <Button 
      android:id="@+id/btnCancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="@dimen/margin50" 
      android:text="Cancel" 
      android:textColor="@color/white_color"/> 

    </RelativeLayout> 
</LinearLayout> 

我我試圖在OK按鈕的背景中設置橙色,但其尺寸發生了變化。我試着用下面的代碼:在繪製對象setColorFilter()不能適用於(INT):

btnOK.getBackground().setColorFilter(getResources().getColor(R.color.orange_color)), PorterDuff.Mode.SRC_ATOP); 

它給我的錯誤。請幫我解決問題

+1

btnOK.setBackgroundColor(getResources()。getColor(R.color.your_color));使用此 –

+0

同樣的問題。增加了按鈕的大小。 –

+0

查看我的回答下面 –

回答

0

如果你想改變背景顏色,但保留其他樣式,那麼下面可能會有所幫助。

btnOK.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.MULTIPLY); 
+0

這是工作,但按鈕的背景顏色不同於我用過的 –

+0

你可以改變你的顏色你想要什麼:)就像我放置R.color.colorAccent你可以在這裏放置你的顏色R.color .orange_color –

+0

,如果它可以幫助你友好地接受答案爲正確:)快樂編碼 –

0

試試這個:

btnOK.setBackgroundColor(getResources().getColor(R.color.orange_color)); 
+0

仍然是同一個問題。 –

0

btnOK.setBackgroundResource(R.color.orange_color);