以编程方式/动态设置样式视图

时间:2017-11-17 16:08:39

标签: android android-layout

我正在构建一个库,并想知道如何以编程方式设置我的视图样式。我做了很长时间的研究,但我还没有找到解决方案。我在styles.xml

中有以下代码
<?xml version="1.0" encoding="utf-8"?><resources>
<style name="LibTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- 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="PreviewStyle" parent="LibTheme">
    <item name="android:background">@color/back_</item>
    <item name="android:textColor">@color/text_</item>
    <item name="android:gravity">center</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:text">@string/preview</item>
    <item name="android:layout_gravity">end</item>
    <item name="android:layout_marginRight">10dp</item>
</style>
<style name="DescriptionStyle" parent="LibTheme">
    <item name="android:background">@color/back_</item>
    <item name="android:textColor">@color/text_</item>
    <item name="android:gravity">center</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:text">@string/desc</item>
</style>

` 这适用于以下代码:

Context themedContext = new ContextThemeWrapper(getActivity(),
            R.style.DescriptionStyle);
    Context themedContext2 = new ContextThemeWrapper(getActivity(),
            R.style.PreviewStyle);
    TextView desc = new TextView(themedContext);
    TextView preview = new TextView(themedContext2);

但是像<item name="android:layout_gravity">end</item>这样的位置属性不起作用,因此视图仍然失真。我怎么能正确地做到这一点?

提前致谢。

0 个答案:

没有答案