0

활동에 안드로이드 버튼의 테두리를 지정할 수 있습니까? 버튼을 계속 사용하여 배경에 테두리를 추가하고 제거하거나 색상을 동적으로 변경하고 싶습니다.
퍼즐 보드는 빨간 테두리를 선택하는 것이 올바르지 않은 경우 선택할 수있는 몇 개의 버튼으로 구성됩니다.안드로이드 버튼의 배경을 유지하면서 테두리를 지정할 수 있습니까?

+0

난 당신이 비트 맵 및 셰이프 레이어 목록을 사용해야 할 것 같아요. 이 게시물을 참조하십시오. 질문과 유사합니다. http://stackoverflow.com/questions/14377156/adding-a-image-to-layerlist-item-with-shape – Opiatefuchs

답변

1

을 사용하십시오. android:src으로 png 이미지를 사용하고 배경 테두리에는 드로어 블 xml을 추가하십시오.

<ImageButton 
    android:id="@+id/button" 
    android:layout_width="75dp" 
    android:layout_height="70dp" 
    android:background="@drawable/border" 
    android:padding="15dp" 
    android:scaleType="fitCenter" 
    android:src="@drawable/your_image" /> 

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@android:color/transparent" /> 
    <stroke 
     android:width="3dp" 
     android:color="@color/stroke_color"></stroke> 
    <corners 
     android:bottomLeftRadius="8dp" 
     android:bottomRightRadius="8dp" 
     android:topLeftRadius="8dp" 
     android:topRightRadius="8dp" /> 
</shape> 

res/drawable/border.xml으로는 programetically 모양의 테두리 색상을 변경 사용하려면

ImageButton button = (ImageButton) findViewById(R.id.button); 
GradientDrawable backgroundGradient = (GradientDrawable) button.getBackground(); 
backgroundGradient.setStroke(5, Color.RED); // set stroke width and stroke color 
관련 문제