Android 将视图转换为扩展视图的视图

Android 将视图转换为扩展视图的视图,android,android-layout,Android,Android Layout,是否有可能在xml资源布局中有一个基本视图,并在膨胀时将其转换为特定视图 例如,具有扩展EditText的名为MyCustomView的自定义视图,以及一些扩展MyCustomView的视图,如MyCustomViewNumber或MyCustomViewPassword,以及如下布局: <com.example.MyCustomView android:layout_width="wrap_content" android:layout_height="wrap_cont

是否有可能在xml资源布局中有一个基本视图,并在膨胀时将其转换为特定视图

例如,具有扩展EditText的名为MyCustomView的自定义视图,以及一些扩展MyCustomView的视图,如MyCustomViewNumber或MyCustomViewPassword,以及如下布局:

<com.example.MyCustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    .....>
</com.example.MyCustomView>
概述:

公共类MyCustomView扩展了EditText

公共类MyCustomViewNumber扩展了MyCustomView{
ctors>this.setInputType(“数字”);
}

公共类MyCustomViewPassword扩展了MyCustomView{ctors>与上面相同}

将MyCustomView充气。将膨胀视图设置为MyCustomViewNumber或MyCustomViewPassword。可能吗


基本上我这样做是因为我需要“layoutParams”。我知道我可以从膨胀视图中获取布局参数,删除它,然后添加带有该参数的新参数。

否,您在XML中使用的完全限定名的类由系统实例化,因此必须是一个具体的类。

我不这么认为。不能将基类强制转换为扩展类。只有反向才有可能。这会扼杀继承的概念,你说得对。但我真的不想那样。我想要的是从扩展类中获取属性集,并将它们交给基类。这有点像,不是吗?基类的对象不能像这样获取其扩展类的属性。我觉得您应该找到另一种方法来实现您正在尝试的目标。您在XML中声明的视图类必须由系统膨胀,因此它必须知道确切的类的名称,而不是接口或抽象类的名称。所以这是不可能的。当然,另一种方法是创建两个独立的类,并尝试将CustomView(您预期的基类)中的共享行为提取到每个实际自定义视图都可以使用的另一个类。
View baseView = LayoutInflater.from(getContext()).inflate(R.id.my_layout, container, false);
baseView = new MyCustomViewNumber(getContext()). //with this line I want that my view from the layout to take all attributes from MyCustomViewNumber.