2017-04-12 59 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”; –

相关问题