[英]How can I define ImageView with height width and source defined on xml file?
Inflating view on fragment causing remove the attributes, how can I define ImageView with height width and source defined on xml file?片段的膨胀视图导致删除属性,我如何定义 ImageView 与 xml 文件上定义的高度宽度和源?
I am using this code bellow.我正在使用下面的代码。 for image_fragment.xml
对于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="test Image"
tools:src="@drawable/cartoon" />
</LinearLayout>
Using kotlin class for the fragment使用 kotlin class 作为片段
ImageFragment.kt图像片段.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.
当我尝试调试工具时,我发现imageview的高度和宽度没有定义,图像的资源也是如此。
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<ImageView>(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?那么如何在 xml 文件中定义属性和图像源呢?
you are using tools
in xml
, it only shows a preview.您正在使用
xml
中的tools
,它只显示预览。
you must use app:srcCompat
:你必须使用
app:srcCompat
:
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="test Image"
app:srcCompat="@drawable/cartoon" />
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).
Android Studio 支持工具命名空间中的各种 XML 属性,这些属性支持设计时功能(例如在片段中显示的布局)或编译时行为(例如应用于 Z3501BB093D363810B67105ED 资源的收缩模式)。 When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
当您构建应用程序时,构建工具会删除这些属性,因此不会影响您的 APK 大小或运行时行为。
so tools:src="@drawable/cartoon"
just shows in designing UI preview and it will be removed at build time .所以
tools:src="@drawable/cartoon"
只显示在设计 UI 预览中,它将在构建时被删除。
use app:srcCompat="@drawable/cartoon"
instead of tools:src="@drawable/cartoon"
使用
app:srcCompat="@drawable/cartoon"
而不是tools:src="@drawable/cartoon"
also for using app:srcCompat
in your XML file, make sure to do the following:还要在 XML 文件中使用
app:srcCompat
,确保执行以下操作:
setup your build.gradle file设置您的build.gradle文件
android {
defaultConfig {
vectorDrawables {
useSupportLibrary = true
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.