2014-12-18 4 views
1

내 Android 앱에는 오른쪽에 화살표 이미지가있는 버튼이 있습니다. 내 SettingsActivity에서 당신은 응용 프로그램의 색상을 변경할 수 있습니다 (버튼과 TextViews의 색상을 변경).Android에서 이미지로 버튼의 배경색을 변경하는 방법

TextViews의 색상을 변경했지만 버튼의 색상을 변경하면 이미지 (오른쪽의 화살표)가 사라졌습니다.

이제 내 코드가되었습니다. 그것은 색상을 변경하고 이미지를 삭제합니다.

loginButton = (Button) findViewById(R.id.btnLogin); 
loginButton.setTextColor(color); 

일부 게시물에이 내용이 있습니다. 그러나 이것은 버튼이 아니라 ImgageView에서만 작동합니다.

loginButton = (Button) findViewById(R.id.btnLogin); 
GradientDrawable bgShape = (GradientDrawable)loginButton.getBackground(); 
bgShape.setColor(getResources().getColor(Color.BLACK)); 
text.setTextColor(color); 

또한 이와 같은 색을 변경하려고합니다. 그러나 이것은 색상을 전혀 변경하지 않습니다.

Drawable button = context.getResources().getDrawable(R.drawable.button); 
button.setColorFilter(new 
PorterDuffColorFilter(0xffff00,Mode.MULTIPLY)); 

마지막으로해야 할 일은 모든 색상에 대해 새로운 드로어 블을 정의하는 것입니다. 그러나 이것은 정말로 끔찍합니다 ... 나는 또한 문제는 이미지와 만이 아니라고 생각합니다. 이 솔루션이 모든 드로어 블 파일을 오버라이드한다고 생각합니다.

드로어 블 색상을 변경하고 이미지를 버튼에 유지하는 방법을 아는 사람이 있습니까?

편집 : 그냥 시도 ... 모든 색상을 colors.xml 파일로 이동할 수 있습니다. 컬러의 경로를 모든 드로어 블 파일로 변경하십시오. Like

<resource> 
    Color 1 
    Color 2 
    Color 3 
    ... 
</resource> 

그러나 어떻게 드로어 블 파일이 어떤 색을 사용해야하는지 결정하는 것보다?

EDIT2 : 버튼 레이아웃 :

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    style="@style/bgGrey"> 
    <RelativeLayout 
     android:layout_height="0dip" 
     android:layout_weight="30" 
     android:layout_width="fill_parent" 
    > 
    <TextView 
     ... 
    /> 
    </RelativeLayout> 
    <LinearLayout 
    ...> 
     <TextView 
     .../> 
     <EditText 
     .../> 
     <TextView 
     .../> 
     <EditText 
     .../> 
     <Button 
      android:id="@+id/btnLogin" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/password" 
      android:gravity="center|left" 
      android:paddingLeft="15dp" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/login_button_background" 
      android:text="Login" /> 

    </LinearLayout> 
</LinearLayout> 

버튼 당김, background.xml : 다른 방법이 될 수

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:left="0dp" android:top="5dp" android:bottom="5dp" android:right="0dp" > 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
      <solid android:color="#ffb500"/> 
     </shape> 
    </item> 
    <item android:left="350dp" android:top="5dp" android:bottom="5dp" android:right="5dp" > 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
      <solid android:color="#ffb500"/> 
     </shape> 
    </item> 
    <item android:right="15dp" > 
     <bitmap android:src="@drawable/btn_right" android:gravity="right" android:tileMode="disabled" /> 
    </item> 
</layer-list> 
+0

레이아웃 XML 및 단추 파일을 드로어 블 폴더에서 게시 할 수 있습니까? –

+0

@blipinsk 물론 레이아웃과 드로어 블이 이미 추가되었습니다. – kristyna

답변

1

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/layer"></item> 
</selector> 

layer.xml :에서 화살표를 제거 배경을 설정하고 Compound Drawable으로 설정합니다.

ButtonTextView으로 확장되며 드로어 블을 위, 아래, 왼쪽 또는 오른쪽으로 설정할 수 있습니다. XML에서

, 그냥 화살표로 재산 drawableRight을 설정하거나 자바 코드를 통해 :

button.setCompoundDrawablesWithIntrinsicBounds (0, 0, R.drawable.your_arrow, 0); 

이 방법은, 배경이 독립적 인 :

button.setCompoundDrawablesWithIntrinsicBounds (idLeft, idTop, idRight, idBottom) 

은 그래서 오른쪽 화살표를 설정 복잡한 버전이나 선택기를 만들지 않고도 쉽게 수정할 수 있습니다.

그리기 가능 패딩을 조정하여 원하는 위치에 배치 할 수있는 방법과 속성이 있습니다.

+0

굉장! 좋아, 고마워. :) – kristyna

관련 문제