在自定义视图中添加ImageView

问题描述 投票:0回答:3

我试图根据用户点击ImageView的位置显示GameView。我在android:layout_height="300dp"上收到错误,消息"Error:(15) error: not well-formed (invalid token)." GameView扩展ViewGroup。我为onMeasure实施了onLayoutonDrawGameView

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

<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>
android viewgroup
3个回答
0
投票
<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的>


0
投票

最终布局应如下所示。

<?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>

0
投票

我认为你有语法错误。 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>
© www.soinside.com 2019 - 2024. All rights reserved.