【问题标题】:Xamarin - Binary XML file line #1: Error inflating class <unknown>Xamarin - 二进制 XML 文件第 1 行:膨胀类 <unknown> 时出错
【发布时间】:2014-10-06 06:27:33
【问题描述】:

我目前正在使用 C# 在 Xamarin 中开发一个小型 Android 应用。
当我在我的模拟器上运行它时,会显示以下内容:

选择第一个选项卡时会调用这行代码,这是默认情况下,这意味着我一运行程序就会发生此错误。在我的一个布局文件中,似乎导致错误的 XML sn-p 是这样的:

android:background="@color/done"

这是在 Dialer.axml 的第 111 行,其全部内容如下。在这里,我试图为我的按钮引用color state list,以便颜色根据是否被触摸而改变。我知道这条线会导致错误,因为从它所在的button 标记中删除它会导致程序完美运行。这是done.xml 的代码,在我的color 文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="#ff2222ff"/>
    <item android:state_pressed="true" android:state_enabled="false" android:color="#ff4444ff"/>
    <item android:state_enabled="false" android:color="#ff000000"/>
    <item android:color="#ff0000ff"/>
</selector>

这是Dialer.axml的代码:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tableLayout1"
    android:background="#2ec0ff">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="50sp"
        android:layout_marginBottom="40sp"
        android:id="@+id/number"
        android:editable="false"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:gravity="right"
        android:textColor="#fff" />
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_weight="1">
        <Button
            android:text="1"
            android:layout_column="0"
            android:id="@+id/button1"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="2"
            android:layout_column="1"
            android:id="@+id/button2"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="3"
            android:layout_column="2"
            android:id="@+id/button3"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_weight="1">
        <Button
            android:text="4"
            android:layout_column="0"
            android:id="@+id/button4"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="5"
            android:layout_column="1"
            android:id="@+id/button5"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="6"
            android:layout_column="2"
            android:id="@+id/button6"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_weight="1">
        <Button
            android:text="7"
            android:layout_column="0"
            android:id="@+id/button7"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="8"
            android:layout_column="1"
            android:id="@+id/button8"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="9"
            android:layout_column="2"
            android:id="@+id/button9"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow4"
        android:layout_weight="1">
        <Button
            android:text="*"
            android:layout_column="0"
            android:id="@+id/buttonStar"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="0"
            android:layout_column="1"
            android:id="@+id/button0"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="#"
            android:layout_column="2"
            android:id="@+id/buttonPound"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </TableRow>
    <Button
        android:text="Done"
        android:id="@+id/buttonDone"
        android:background="@color/done"
        android:layout_width="match_parent"
        android:layout_height="80sp"
        android:textSize="35sp"
        android:layout_margin="5sp" />
</TableLayout>

是什么导致了错误?我是否错误地引用了done.xml?我是不是放错文件夹了?

提前致谢。

【问题讨论】:

  • 你能包含Resource.Layout.Dialer的布局吗?
  • @MattR 很抱歉没有立即这样做 - 我刚刚添加了它。

标签: c# android xml xamarin


【解决方案1】:

您的 done.xml 是颜色状态列表,但颜色状态列表不能用作背景。您需要使用 state-list-drawable 作为背景。

这是您的颜色状态列表的 state-list-drawable 版本:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="#ff2222ff" /> 
        </shape>
    </item>
    <item android:state_pressed="true" android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="#ff4444ff" /> 
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="#ff000000" /> 
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ff0000ff" /> 
        </shape>
    </item>
</selector>

将其放入可绘制文件中的 xml 文件中,并使布局按钮 xml 如下所示:

android:background="@drawable/done"

您可以在此链接中找到类似的问题: How to set custom button state background color?

【讨论】:

  • 谢谢你,这行得通!但是,对于第二项,我必须将 android:state_enabled 更改为“true”,否则颜色根本不会改变。
  • 是的,我意识到情况确实如此,但不想改变这种想法,认为这可能是故意的 :)
猜你喜欢
  • 2018-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
相关资源
最近更新 更多