2012-10-18 6 views
0
<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle">  
    <solid android:color="#EAEAEA"/>  

    <corners android:bottomLeftRadius="5dip" 
       android:topRightRadius="5dip" 
       android:topLeftRadius="5dip" 
       android:bottomRightRadius="5dip" 
    /> 
</shape> 

내 버튼의 배경에 내 gradient image을 어떻게 설정합니까? 속성 그라디언트가 표시되지만 배경에 속성을 표시 할 수 없습니다.Android : 버튼에 맞춤 그라데이션 설정

참고 : 저는 아주 새로운 Android 개발 제품입니다.

답변

3

당신이 우리에게 보여준 XML이 그라디언트와 관련이 있는지 확실하지 않습니다. 당신이 가질 수

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient 
     android:startColor="#FFFFFFFF" 
     android:endColor="#FFD9D9D9" 
     android:angle="270" 
    /> 
    <corners android:bottomLeftRadius="5dip" 
      android:topRightRadius="5dip" 
      android:topLeftRadius="5dip" 
      android:bottomRightRadius="5dip" 
    /> 
</shape> 

(예를 들어, my_gradient.xml으로 저장) 레이아웃 XML 파일에서 다음

:

<Button android:id="@+id/ButtonStart" 
    android:layout_width="100dp" android:layout_height="wrap_content" 
    android:background="@drawable/my_gradient" 
    android:textColor="@color/white" android:textSize="14sp" 
    android:textStyle="bold" android:text="@string/game_start"/> 
2

그라디언트를 XML로 정의하거나 이미지 (둥근 모서리 포함)를 사용해야합니다. XML 모양과 이미지를 쉽게 혼합 할 수는 없습니다 (최소한 초보자 인 경우 간단한 재료를 먼저 사용하는 것이 좋습니다). 예를 들어

:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient 
     android:startColor="#474946" 
     android:endColor="#181818" 
     android:angle="270"/> 
    <corners android:radius="5dp" /> 
</shape> 

는 그런 다음 사용하여 버튼의 배경을 정의 할 수 있습니다 android:background="@drawable/bg_custom_button"

당신은 그들이 당신이 당신의 배경에 대한 strechable 이미지를 정의 할 수 있습니다 당신을 절약 할 수 약 아홉 패치를 배워야한다

디자인이 XML로 실현 될 수 없을 때.

2
당신은 당신의 drawable 폴더에 XML 파일의 그라데이션을 정의 할 수 있습니다

모양이 올바른 방향이지만 그래디언트를 사용할 수있는 단색 대신

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle">  
    <gradient 
     android:angle="270" 
     android:endColor="@color/gradient_bottom" 
     android:startColor="@color/gradient_top" />  

    <corners android:bottomLeftRadius="5dip" 
      android:topRightRadius="5dip" 
      android:topLeftRadius="5dip" 
      android:bottomRightRadius="5dip" 
    /> 
</shape> 

위의 모양이 gradient_background.xml로 저장되었다고 가정하고 드로어 블 폴더에 저장해야합니다. 이제이 드로어 블을 버튼 배경으로 사용할 수 있습니다.

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/gradient_background" 
    android:text="Button" /> 
관련 문제