Is there a tools attribute for ImageView content in Android?

时光怂恿深爱的人放手 提交于 2020-05-26 11:09:40

问题


I could not find a tools attribute to set an example image in a layout for Android. My assumption is that there is no such thing at the moment, at least no direct support.

But I was wondering whether I just did not search well enough or whether there is a workaround for this.

How do you define image placeholders at design time in Android with tools?


Example of a tools attribute for a TextView (i.e. the tools:text attribute for text):

<TextView
    android:id="@+id/tv_vote_average"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textStyle="bold"
    tools:text="8.4/10"/>

回答1:


It's just straightforward like for the TextView. Just use tools:src to set the image.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:src="@drawable/placeholder" />



回答2:


Use this for random real sample images:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    tools:src="@tools:sample/backgrounds/scenic" />



回答3:


tools:background works for me too.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:background="@mipmap/ic_launcher"/>
</LinearLayout>




回答4:


Tips:

  1. Write toolNS or tools on the root of your layout XML and 'ALT+ENTER' to complete the name space statement there. Same works with appNS, android NS is by default mentioned if you generate layout using new-->layout resource.

  2. Many attributes are not documented in the link : Tools Namespace Attributes, however these are same as those declared with androidNS (android:) for the current view or layout tag.

Happy Coding :-)



来源:https://stackoverflow.com/questions/42050241/is-there-a-tools-attribute-for-imageview-content-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!