我们如何以编程方式添加样式? [英] How we add styles programmatically?

查看:69
本文介绍了我们如何以编程方式添加样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为TextView编写了一种样式,并且为TextView设置了一种样式,因为style="@style/popup_window_text_style"可以正常工作,没有问题.

I have written one style for TextView and I set that one for my TextView as style="@style/popup_window_text_style" working fine, no issues.

现在,我希望我的textview以编程方式执行相同的样式

Now I want that same style to do in programmatically for my textview

TextView textView = new TextView(mContext);

有什么方法可以用Java编程吗?

Is there any way to do it programmatically in Java?

styles.xml中的样式

My style in styles.xml

<style name="popup_window_text_style">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:minWidth">60dp</item>
        <item name="android:padding">10dp</item>
        <item name="android:layout_marginLeft">1dp</item>
        <item name="android:layout_marginRight">1dp</item>
        <item name="android:background">@drawable/ctxmenu_btn_selector</item>
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">16sp</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:singleLine">true</item>
    </style>

推荐答案

这应该对您有用:

textView.setTextAppearance(getApplicationContext(), R.style.popup_window_text_style);

但是请注意,某些属性可能不会受到影响,可能必须手动设置

But note that some properties may not be affected and may have to be set manually as specified in the Android developer docs.

不幸的是,当前的SDK似乎缺少任何形式的setStyle()方法.

Unfortunately the current SDK appears to lack any form of a setStyle() method.

这篇关于我们如何以编程方式添加样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆