请注意,我正在为 Android 使用 Mono
我有一个项目 ListView ,可以通过单击列表底部的“更多”按钮来扩展这些项目。该列表是在 XML 中定义的,但更多按钮只是一个可单击的 TextView,它通过 AddFooterView 添加到列表的底部。我正在尝试将预定义样式应用于运行时创建的 TextView,但它不起作用。
mMoreProductsButton = new TextView(this, null, Resource.Style.more_button);
mMoreProductsButton.Text = "More";
mMoreProductsButton.Click += new EventHandler(MoreProductsButton_Click);
mListView.AddFooterView(mMoreProductsButton);
TextView 文档显示此签名:TextView(Context context, AttributeSet attrs, int defStyle)
defStyle
在文档中定义为应用于 View 的默认样式。我在别处看到的一个示例将 null 作为 AttributeSet 传递,但我不确定这是否有效或是否与我的问题相关。
我发现了这个错误,但不确定它是否相关并且是最新的: http://code.google.com/p/android/issues/detail?id=12683
如有任何建议,我们将不胜感激!
最佳答案
我也搜索了这个问题的答案。这似乎是一个 Android 错误,因为我没有找到一种将样式应用于创建 View 的方法。
你为什么不想膨胀你的 TextView?
像这样:
layout/my_view.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:style="@style/more_button" />
Activity
TextView mMoreProductsButton = (TextView) getLayoutInflater().inflate(R.layout.my_view, null, false);
抱歉,我不知道它是否适用于 Mono。
关于c# - Android TextView defStyle 参数被忽略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7772472/