如何在 XML 文件中定义具有指定高度、宽度和来源的 ImageView?

huangapple 未分类评论42阅读模式
英文:

How can I define ImageView with height width and source defined on xml file?

问题

在片段上膨胀视图会导致属性丢失,我如何在 XML 文件中定义具有高度、宽度和源的 ImageView?

我正在使用下面的代码,用于 image_fragment.xml

<?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"
    tools:context=".ui.main.ImageFragment"
    android:orientation="horizontal" >
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="测试图片"
        tools:src="@drawable/cartoon" />
</LinearLayout>

使用 Kotlin 类来编写片段:

ImageFragment.kt

class ImageFragment : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val root = inflater.inflate(R.layout.image_fragment, container, false)
        return root
    }
}

它什么都没有显示。当我尝试使用调试工具时,我发现 ImageView 的高度和宽度没有定义,所以图片的资源也没有定义。

只有当我使用:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val root = inflater.inflate(R.layout.image_fragment, container, false)
        root.findViewById<ImageView>(R.id.imageView).setImageResource(R.drawable.cartoon)
        return root
    }

它才显示图片。但是属性仍然丢失。

那么,我应该怎么做才能在 XML 文件中定义属性和图片源呢?

英文:

Inflating view on fragment causing remove the attributes, how can I define ImageView with height width and source defined on xml file?

I am using this code bellow. for image_fragment.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.ui.main.ImageFragment&quot;
    android:orientation=&quot;horizontal&quot; &gt;
    &lt;ImageView
        android:id=&quot;@+id/imageView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:contentDescription=&quot;test Image&quot;
        tools:src=&quot;@drawable/cartoon&quot; /&gt;
&lt;/LinearLayout&gt;

Using kotlin class for the fragment

ImageFragment.kt

class ImageFragment : Fragment() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment

        val root = inflater.inflate(R.layout.image_fragment, container, false)


        return root
    }

}

It showing nothin. When I tried debugging tools, I found that, the height and width of the imageview is not defined so is resource of the image.

only if I use

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment

        val root = inflater.inflate(R.layout.image_fragment, container, false)
        root.findViewById&lt;ImageView&gt;(R.id.imageView).setImageResource(R.drawable.cartoon)


        return root
    }

it is showing the image. But attributes are still missing.

So what should do to define attributes and image source in xml file?

答案1

得分: 0

你正在使用 xml 中的 tools,它只显示一个预览。

你必须使用 app:srcCompat

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="测试图片"
    app:srcCompat="@drawable/cartoon" />
英文:

you are using tools in xml, it only shows a preview.

you must use app:srcCompat:

&lt;ImageView
    android:id=&quot;@+id/imageView&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:contentDescription=&quot;test Image&quot;
    app:srcCompat=&quot;@drawable/cartoon&quot; /&gt;

答案2

得分: 0

正如文档所述

> Android Studio支持在工具命名空间中使用各种XML属性,以实现设计时功能(例如在片段中显示哪个布局)或编译时行为(例如对XML资源应用哪种缩小模式)。构建应用程序时,构建工具会移除这些属性,因此不会影响APK的大小或运行时行为。

因此,tools:src=" @drawable/cartoon" 仅会在设计UI预览中显示,并且会在构建时移除
请使用 app:srcCompat=" @drawable/cartoon" 替代 tools:src=" @drawable/cartoon"

另外,如果要在您的XML文件中使用 app:srcCompat,请确保执行以下操作:
配置您的 build.gradle 文件

android {
  defaultConfig {
    vectorDrawables {
      useSupportLibrary = true
    }
  }
}
英文:

as document said

> Android Studio supports a variety of XML attributes in the tools
> namespace that enables design-time features (such as which layout to
> show in a fragment) or compile-time behaviors (such as which shrinking
> mode to apply to your XML resources). When you build your app, the
> build tools remove these attributes so there is no effect on your APK
> size or runtime behavior.

so tools:src=&quot;@drawable/cartoon&quot; just shows in designing UI preview and it will be removed at build time.
use app:srcCompat=&quot;@drawable/cartoon&quot; instead of tools:src=&quot;@drawable/cartoon&quot;

also for using app:srcCompat in your XML file, make sure to do the following:
setup your build.gradle file

android {
  defaultConfig {
    vectorDrawables {
      useSupportLibrary = true
    }
  }
}

huangapple
  • 本文由 发表于 2020年7月23日 19:00:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/63052703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定