【问题标题】:Adding an ImageView in a custom view在自定义视图中添加 ImageView
【发布时间】:2018-03-04 18:57:28
【问题描述】:

我正在尝试根据用户在GameView 中的点击位置显示ImageView。我在android:layout_height="300dp" 上收到错误消息"Error:(15) error: not well-formed (invalid token)." GameView extends ViewGroup。我为GameView 实现了onMeasureonLayoutonDraw

悬停在这些行的末尾会显示以下错误提示:

<com.example.GameView -- 多个根标签

android:layout_height="300dp" -- 标签开始没有关闭

<ImageView -- 多个根标签

<com.example.GameView> -- 意外令牌

</LinearLayout> -- 意外令牌

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    <com.example.GameView
        android:layout_width="match_parent"
        android:layout_height="300dp"

        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            />
        </com.example.GameView>

</LinearLayout>

【问题讨论】:

  • 这是您的确切布局吗?布局中包含错误。请使用您所拥有的确切布局更新您的问题。
  • 检查这个问题。 stackoverflow.com/a/7089751/2751547
  • 为什么把 放在 里面?
  • @ReazMurshed 是的,这是准确的布局。我已经用悬停时的确切错误更新了它
  • @MeosCoder 根据用户在GameView 中点击的位置,ImageView 应该会出现

标签: android viewgroup


【解决方案1】:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

你错过了 LinearLayout 的&gt;

【讨论】:

    【解决方案2】:

    最终布局应如下所示。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <com.example.GameView
            android:layout_width="match_parent"
            android:layout_height="300dp">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </com.example.GameView>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      我认为您有语法错误。 com.example.GameView 未正确关闭。应该是这样的。

       <com.example.GameView
          android:layout_width="match_parent"
          android:layout_height="300dp">
      
          <ImageView
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              />
      </com.example.GameView>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-23
        • 2013-01-31
        • 1970-01-01
        • 2011-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多