通过styles.xml 向动态Xamarin.Android 控件添加属性

问题描述

我正在尝试将样式属性添加到 Xamarin.Android 中动态创建的控件,类似于如何从 WPF 中的资源应用它们。

例如,我有下面的表格行和一些按钮的 axml(放置在线性布局中)

<TableRow
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#5292C7"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="Example Couple Name Here"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:gravity="center_vertical|left"
            android:layout_weight="1"/>

        <Button
            android:text="Expand"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="10dp"/>

    </LinearLayout>

</TableRow>

(渲染时看起来像这样)

enter image description here

我能够创建控件并将它们与诸如此类的代码组合在一起,但从逻辑上讲,必须有比在代码中单独手动应用每个样式属性更好的方法

// Top Row
TableRow tableRow = new TableRow(this);
topRow.AddView(tableRow);

// Couple Text
TextView coupleText = new TextView(this);

coupleText.Text = "Example Couple Name Here";
coupleText.TextSize = 15;

TableLayout.LayoutParams coupleTextLayoutParams = new TableLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent,LinearLayout.LayoutParams.WrapContent);
coupleTextLayoutParams.LeftMargin = 10;
coupleTextLayoutParams.Gravity = GravityFlags.CenterVertical|GravityFlags.Left;
coupleText.LayoutParameters = coupleTextLayoutParams;
coupleText.LayoutParameters = new TableRow.LayoutParams(1);

tableRow.AddView(coupleText);

要应用的示例样式类似于来自 style.xml 的内容

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <style name="CustomTextViewStyle">
   <item name="android:text">Example Couple Name Here</item>
   <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
   <item name="android:layout_width">wrap_content</item>
   <item name="android:layout_height">match_parent</item>
   <item name="android:layout_marginLeft">10dp</item>
   <item name="android:gravity">center_vertical|left</item>
   <item name="android:layout_weight">1</item>
  </style>
</resources>

可以用这样的东西来应用......(理论上)

// Couple Text
TextView coupleText = new TextView(this);

coupleText.style = Resources.Style("CustomTextViewStyle");

tableRow.AddView(coupleText);

这是存在的东西还是通过代码动态完成的唯一方法

解决方法

您可以使用 SetTextAppearance 动态设置样式

先设置id。

    <TextView
        android:id="@+id/textView1"
        android:text="Example Couple Name Here"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical|left"
        android:layout_weight="1"           
        style="@style/CustomTextViewStyle"/>

然后使用下面的代码。

var textview = FindViewById<TextView>(Resource.Id.textView1);
        textview.SetTextAppearance(Resource.Style.CustomTextViewStyle);

如果你动态创建控件,你可以使用下面的代码。

TextView coupleText = new TextView(this);
        coupleText.SetTextAppearance(Resource.Style.CustomTextViewStyle);

更新:

var tablelayout = new TableLayout(new ContextThemeWrapper(this,Resource.Style.CustomTextViewStyle));
        var tableRow = new TableRow(new ContextThemeWrapper(this,Resource.Style.CustomTextViewStyle)));