2017-04-12 57 views
0

我想使用setTextAppearance更改按鈕的樣式,但拋出「depreciated」.how以正確使用setTextAppearance?以編程方式更改api小於23的樣式

styles.xml

<resources> 
 

 
    <!-- Base application theme. --> 
 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
 
     <!-- Customize your theme here. --> 
 
     <item name="colorPrimary">@color/colorPrimary</item> 
 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
 
     <item name="colorAccent">@color/colorAccent</item> 
 
    </style> 
 

 
    <style name="button_custom" parent="Theme.AppCompat.Light"> 
 
     <item name="colorControlHighlight">#FFF59D</item> 
 
     <item name="colorButtonNormal">#FFF59D</item> 
 
    </style> 
 

 
</resources>

而且活動

公共類MainActivity擴展AppCompatActivity {

Button btn; 
Context context; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btn = (Button) findViewById(R.id.button); 
    context = this; 

    btn.setTextAppearance(context,R.style.button_custom); 

} 

public void setTextAppearance(Context context, int resId) { 
    if (Build.VERSION.SDK_INT < 23) { 
     super.setTextAppearance(context, resId); 
    } 
    else { super.setTextAppearance(resId); 
    } 
} 

}

回答

0

你可以嘗試讓你的AppCompatButton類型的按鈕,使用

btn.setTextAppearance(resid) 

它還將幫助您避免Build.VERSION.SDK_INT分支

您可以按照本文檔https://developer.android.com/reference/android/support/v7/widget/AppCompatButton.html#setTextAppearance(android.content.Context,INT)

用戶AppCompatButton

<android.support.v7.widget.AppCompatButton 
     android:id="@+id/some_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
+0

btn.setTextAppearance(resid)不適用於API小於23.我檢查d但是我無法找到答案。 –

+0

我更新了我的答案部分。你可以使用它,看看它是否有幫助 –

+0

我嘗試使用AppCompatButton並在setTextAppearance(context,resId)上說「depreciated」; –

相關問題