2012-05-21 3 views
7

사용자 정의 버튼의 기본 배경을 null로 설정하려면 어떻게합니까? "@null"에 을 배경 및 명시 적으로 자신의 레이아웃에 스타일을 적용하도록 요청 :사용자 정의보기에서 기본 스타일 (속성) 제공

내 말은 ... 는 내가 안드로이드를 설정 "스타일"을 정의 할 수 있습니다 알고있다. 예를 들어 : 코드 위

<style name="MyButton" parent="@android:style/Widget.Button"> 
    <item name="android:background">@null</item> 
</style> 

<com.xxx.widget.MyButton 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="@style/MyButton" 
    android:text="MyButton" /> 

잘 노력하고 있습니다. 그러나 내 클래스 "MyButton"에서이 스타일을 내부적으로 적용하고 사용자가 스타일을 명시 적으로 설정하지 못하게하려면 어떻게해야합니까?

<com.xxx.widget.MyButton 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="MyButton" /> 

내가 다음과 같이 생성자에서이 작업을 수행하려고하지만 작동하지 :

예를 들어, 만드는 방법 레이아웃 다음은 이전과 같이 작동합니다.

public MyButton(Context context, AttributeSet attrs) { 
    this(context, attrs, com.xxx.R.style.MyButton); 
} 

ps. 사용자가 배경을 명시 적으로 설정하지 않은 경우이 "null"배경을 적용하려고합니다.

+0

난 당신이 질문의 제목을 변경한다고 생각합니다. 기본 스타일은 언제든지 가질 수 있지만 나중에 변경할 수 있습니다. 아마도 당신은 '최종 스타일'또는 이와 비슷한 말을하기를 좋아할 것입니다. – ikbal

+1

이것에 대한 몇 가지 팁은 http://stackoverflow.com/questions/4493947/how-to-define-theme-style-item-for-custom-widget을 참조하십시오. –

+0

비슷한 요구 사항이 있음 : http://stackoverflow.com/questions/31504413/theme-style-for-custom-view/31505624#31505624 – stefan

답변

0

MyButton() 생성자에서 setBackground (null)을 호출하지 않는 이유는 무엇입니까?

0

이 방법은 다소 비용이 적게 들지만 배경을 투명 사각형으로 설정하지 않는 이유는 무엇입니까? 이처럼 ->

transparent_square.xml :

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/square" 
    android:> 

    <solid android:color="#00808080"></solid> // The first two zeros set the alpha 
               // to 0 

</shape> 

는 그런 다음 당김에있는 버튼의 배경을 설정 (드로어 블 디렉토리에 넣어).

버튼 :

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send" 
     android:id="@+id/sendButton" 
     android:layout_weight="1" 
     android:background="@drawable/transparent_square"/> 
관련 문제