2017-04-08 10 views
0

Button 배경색을 프로그래밍 방식으로 단추 모양 (타원형)으로 변경하고 싶습니다. XML에서 변경되었습니다. 타원형 단추 배경을 프로그래밍 방식으로 변경

코드입니다 :

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <corners android:radius="20dp"/> 
     <solid android:color="#f9f9f9"/> 
     <stroke 
      android:width="2dp" android:color="#FFFF4917" /> 
      </shape> 

나는이 Button을 변경하지만 문제는 버튼의 색상 변화하고 있다는 점이다하는 Button.backgroundColor(COLOR.RED)를 사용하고 있지만, 내 버튼을 직사각형 형태 (A의 기본 모양을하고있다 버튼)

답변

1

당신은

GradientDrawable BackgroundShape = (GradientDrawable)btn.getBackground(); 
BackgroundShape.setColor(Color.BLACK); 
,369 아래에이 간단한 방법을 사용하여 수정할 수 있습니다
+0

덕분에 10 분 후에 투표를 working.will – Meghna

+0

코딩 해피 :) Btw은 내가, 당신은 당신의 원본에 맞춤법 실수를 한 질문을 편집했다. – Rab

0

사용하십시오. 버튼을 클릭하면 사각형 모양이 타원형 모양으로 변합니다.

style_login_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" > 
     <shape android:shape="oval"> 
      <solid android:color="@color/colorDeepOrange"/> 
      <size android:width="120dp" android:height="120dp"/> 
     </shape> 
    </item> 
    <item android:state_focused="true"> 
     <shape android:shape="oval"> 
      <solid android:color="@color/colorOrange"/> 
      <size android:width="120dp" android:height="120dp"/> 
     </shape> 
    </item> 
    <item > 
     <shape android:shape="oval"> 
      <solid android:color="@color/colorOrange"/> 
      <size android:width="120dp" android:height="120dp"/> 
     </shape> 
    </item> 
</selector> 

그리고 Button에 적용됩니다. 마찬가지로 -

<Button 
       android:id="@+id/btnSignin" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/text_btn_login" 
       android:textColor="@color/colorWhite" 
       android:textSize="25dp" 
       android:padding="30dp" 
       android:textAllCaps="false" 
       android:layout_marginTop="20dp" 
       android:layout_marginBottom="20dp" 
       android:background="@drawable/style_login_button"/> 
+0

코드를 사용하여 프로그래밍 방식으로 단추를 타원형으로 설정하려고합니다. – Rab