2017-09-23 13 views
0

android.widget.Button을 기반으로 자신 만의 버튼을 만들려고합니다. XML에서프로그래밍 방식으로 스타일 속성을 설정할 때 현재 테마에서 스타일 'My_Button'을 찾지 못했습니다.

그러나 설정 스타일 작품 현재 테마에서 스타일 'my_button이라는'를 찾는 데 실패

: 나는 프로그래밍 스타일 속성을 설정하지만,이 렌더링 문제를 얻고있다.

Screenshot

오전 내가 모르는 뭔가가?

P/s : 영어는 제 모국어가 아닙니다. 제발 착오를 저 지르십시오.


activity_main.xml

<com.myapp.Components.Button 
    android:id="@+id/xx" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:onClick="touch_" 
    app:_icon=""/> 
<com.myapp.Components.Button 
    style="@style/My_Button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:onClick="touch" 
    app:_icon=""/> 

Button.java

public class Button extends android.widget.Button { 
    public Button(@NonNull Context context) { 
     super(context, null, R.style.My_Button); 
     init(context, null); 
    } 

    public Button(@NonNull Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs, R.style.My_Button); 
     init(context, attrs); 
    } 
    public Button(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { 
     super(context, attrs, R.style.My_Button); 
     init(context, attrs); 
    } 
    // ... more 
} 

,

styles.xml

<style name="My_Button" parent="Base.Widget.AppCompat.Button.Borderless"> 
    <item name="android:foreground">?android:attr/selectableItemBackground</item> 
    <item name="android:background">#592612</item> 
    <item name="android:gravity">center</item> 
</style> 

답변

0

마지막으로, 나는 답을 발견했다!

교체 :

super(context, attrs, R.style.My_Button);super(context, attrs, R.attr.ButtonStyle);

styles.xml 에서 함께 추가attrs.xml이에서

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="ButtonStyle">@style/Button_Style</item> 
    ... 
</style> 

를 추가

<declare-styleable name="Theme"> 
    <attr name="ButtonStyle" format="reference"/> 
</declare-styleable>