更新时间:2023-11-20 21:51:34
您可以使用布局填充器,以便自动"应用您可以定义的样式.
You can use the layout inflater in order to "auto-magically" apply the styles you can defined.
让我们假设您的Spinner
具有全局"风格:
Lets assume you have a "global" style for your Spinner
:
<style name="MySpinnerStyle" parent="android:Widget.Spinner">
<item name="android:spinnerMode">dialog</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">40dp</item>
<item name="android:layout_marginBottom">8dp</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
<item name="android:paddingStart">8dp</item>
<item name="android:paddingEnd">8dp</item>
<item name="android:background">@drawable/myspinner</item>
<item name="android:prompt">@string/project_prompt</item>
</style>
为使用全局Spinner样式创建的动态Spinner创建独立的Layout
Create an independent Layout
for the dynamic Spinner(s) that you create that uses your global Spinner style
<?xml version="1.0" encoding="UTF-8" ?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/masterSpinner"
style="@style/MySpinnerStyle" />
现在,不要实例化新的Spinner
,而是膨胀 layout ,然后将分配所有关联的属性:
Now instead of instancing a new Spinner
, inflate the layout and all the associated properties will be assigned:
var spinner = LayoutInflater.Inflate(Resource.Layout.Spinner, rootLayout, false) as Spinner;
spinner.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, new string[] { "Stack", "Over", "Flow" });
customAttributeHolder.AddView(spinner, StackerPosition);