android 怎么動態設置button 的style


網上找了很多,還是沒有直接的解決辦法,button沒有setstyle這個方法。因此我的解決辦法如下:

直接動態設置各個屬性

Button themeBtn = new Button(this);
            themeBtn.setText(entry.getKey());
            LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
            themeBtn.setLayoutParams(lparams);
            themeBtn.setTextAppearance(this, R.style.ImageCategoryThemeBtn);
            themeBtn.setPadding(DensityUtils.dp2px(this, 8), 0, DensityUtils.dp2px(this, 8), 0);
            themeBtn.setBackgroundResource(R.drawable.selector_bt_gray2blue);
            v_theme.addView(themeBtn);

 

style:

<style name="ImageCategoryThemeBtn" parent="@android:style/Widget.Button">
        <item name="android:textColor">@color/image_theme</item>
        <item name="android:textSize">@dimen/imageThemeBtnTextSize</item>
    </style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM