【问题标题】:I get a framelayout error I can't add an image我收到框架布局错误我无法添加图像
【发布时间】:2021-01-30 10:24:03
【问题描述】:

我想在我的 Android 应用程序中显示一张图片。

如您所见,我将ImageView 放入我的布局中并使用android:src="@drawable/android" 引用它

图片路径是MyApp6/app/src/main/res/drawable/android/a.jpeg,我确定是正确的。

但是,在构建项目后,我收到了以下错误消息

No resuorce found that matches the given name (at 'src' with value @drawable/android.")

我没有电脑,所以我使用助手。

这是我的 XML 布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff000"
        android:gravity="center_horizontal"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/tw2"
            android:textSize="30dp"
            android:layout_margin="16dp"
            android:text="Güzel Sözler"
            android:shadowColor="#00ccff"
            android:shadowRadius="2"
            android:shadowDx="1"
            android:shadowDy="1"
            android:textIsSelectable="true"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
            
        <TextView
            android:id="@+id/tw1"
            android:textSize="15dp"
            android:layout_margin="16dp"
            android:text=""
            android:shadowColor="#00ccff"
            android:shadowRadius="2"
            android:shadowDx="1"
            android:shadowDy="1"
            android:textIsSelectable="true"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        
        <Button 
            android:id="@+id/btn1"
            android:text="random"
            android:textSize="20dp"
            android:background="#cae1ff"
            android:textColor="#ff3030"
            android:layout_centerHorizontal="true"
            android:layout_gravity="bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
            
        <FrameLayout
            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"
            tools:context=".MainActivity">
        
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/android"/>
        
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="20dp"
                android:gravity="center_horizontal|bottom"
                android:text="Android Programlama"
                android:textColor="#ffffff"
                android:textSize="30sp"
                android:textStyle="bold"/>
        
        </FrameLayout>
    
    </LinearLayout>

【问题讨论】:

  • 首先你需要确保你已经在你的drawable文件夹中添加了图片a.jpeg。如果您已正确添加,则无需使用@drawable/android 访问图像,而是使用@drawable/a,因为图像文件的名称是a.jpeg,而不是android.jpeg。
  • 很遗憾,同样的错误还在继续

标签: java android xml android-studio android-layout


【解决方案1】:

而不是使用:

android:src="@drawable/android"

使用:

android:src="@drawable/a"

这是因为你的路径是MyApp6/app/src/main/res/drawable/android/a.jpeg 并且您之前放置了文件夹的名称而不是实际图像。

【讨论】:

  • 尝试清理项目,重建项目,清除缓存/重启。
【解决方案2】:

我认为您正在使用文件资源管理器添加图像。不要创建子文件夹。如果您想使用文件资源管理器添加图像,请使用以下路径:

MyApp6/app/src/main/res/drawable/a.jpeg 

代替:

MyApp6/app/src/main/res/drawable/android/a.jpeg

或者您可以直接在 Android Studio 软件上复制图像并将其粘贴(或拖放)到可绘制资源文件夹中。两者都会将您的图像放在同一路径中。

然后将图像设置为 ImageView。

android:src="@drawable/a"

【讨论】:

  • 那么如何在图片中间打印呢?我会设置一张新照片。全屏图片应该多大?
  • “在图片中间打印”是什么意思?关于您的另一个问题,如果您想要全屏图像,您可以将 ImageView 的宽度和高度属性设置为 match_parent (正如您已经设置的那样)但是在您的布局中,您已经有另一个视图,因此您的 ImageView 不能全屏。
  • i.resimyukle.xyz/NeT7VS.jpg 我要全屏。
  • 我认为您希望您的图像作为背景。如果是这样,您不需要使用 ImageView。您可以将图像设置为 LinearLayout 的背景。您的 LinearLayout 的背景是: android:background="#fff000" 将其更改为 android:background="@drawable/a" 并且您的图像将被设置为背景
  • 或者如果您仍然想使用ImageView和图像作为全屏而不将图像设置为背景,您可以将您的父布局更改为ConstraintLayout并设置ImageView的约束。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多