最近我花了一个多小时来弄清楚我的 xml 中出了什么问题......直到我意识到 android xml 中有 2 个不同的小部件:“view”和“View”。两者都是正确的,但并不相同,因为大写 View 给了我有关 classcastException 的错误,直到我将其更改为小写......我们什么时候使用它们中的每一个?
编辑: 因此,为了在 android XML 中创建现有小部件的修改版本,我必须使用小写的“view”:
<view
android:id="@+id/mybutton1"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="75"
class="com.example.customui2.MainActivity$ArticleFragment$MyButton"
android:contentDescription="@string/logo_desc"
android:src="@drawable/menu_button" />
附加到小部件类:
public static class MyButton extends ImageView
{
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
};
当使用大写 View 时,我在这样的行中遇到了异常:
MyButton button1 = (MyButton) rootView.findViewById(R.id.mybutton1);
但我也发现了使用大写 View 的示例,例如“spacer widget”:
<View
android:layout_weight="20"
android:layout_width="match_parent"
android:layout_height="0dp"/>
最佳答案
查看
指android.view.View
它是所有 View 的父类(super class)。它本身不绘制任何东西,但经常用作间隔符。
view
指任何 View
,具体类在 class
属性 class="com.example.在您的情况下,customui2.MainActivity$ArticleFragment$MyButton"
。您也可以将其写为
<com.example.customui2.MainActivity.ArticleFragment.MyButton ...
(尽管通过评论中发布的 Mike M. 链接,我不能 100% 确定它确实适用于这样的内部类,而且我这里没有开发环境来尝试)
关于java - android xml中小写和大写 "view"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24808314/