【发布时间】:2017-12-23 10:37:10
【问题描述】:
帮助!在 VS2017 上使用 Xamarin,我正在努力实现这一目标:
我可以在任何地方使用的 MyHeading 类,就像普通的 TextView 类一样,除了任何 MyHeading 实例都将继承我仅为 MyHeading 定义的填充/边距/样式/字体等。
我迷路了,运气不好,在 Google 上找不到任何有用的东西。
我创建了继承自 TextView 的 MyHeading.cs:
public class MyHeading : TextView
{
public MyHeading(Context context) : base(context)
{
}
}
然后我还在包含此 XML 的 Resources/layout 文件夹中创建了 myheading.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.my.app.MyHeading
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/mygrey"
android:textSize="18dp"
android:textStyle="bold"
android:typeface="sans"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" />
请帮忙!我怎样才能实现这样一个简单的要求,即在我使用它时拥有一个具有相同样式的可重复使用的 TextView。
使用我上面的代码,项目成功构建,但是当我运行它时,它会出现以下问题:
Java.Lang.NoClassDefFoundError: android.support.v7.appcompat.R$drawable
【问题讨论】:
-
我建议宁愿使用 Android style xml attribute,它是共享通用样式的更常见模式。至于您遇到的问题,我相信您需要添加缺少的构造函数重载并夸大您的自定义布局。
标签: android inheritance xamarin xamarin.android