Is there a tools attribute for ImageView content i

2020-08-12 23:18发布

问题:

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 :-)