我们如何以编程方式添加样式?

我们如何以编程方式添加样式?

问题描述:

我为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.