简体   繁体   中英

Imagebutton not showing in Android Emulator

I have a problem whose solution I am unable to find out. I am creating an imagebutton thorugh Android SDk and although it shows in Preview window it does not show in Nexus 6 Emulator. The image is attached here 在此处输入图片说明

Code for xml file is

<?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"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".ViewActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="@dimen/activity_bold_textsize"
    />


    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:src="@tools:sample/avatars" />

    <CheckBox
        android:id="@+id/checkBox"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/rememberMe" />

    <RadioGroup
        android:id="@+id/radio_g1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <RadioButton
            android:id="@+id/rad1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rad1"/>
        <RadioButton
            android:id="@+id/rad2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rad2"
            />
    </RadioGroup>

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/togb1" />

    <Switch
        android:id="@+id/switch1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Switch" />

</LinearLayout>

You do not known the diffenrence between tools and android . tools:src="@tools:sample/avatars" is only show on preview of IDE. Change to android:src="@tools:sample/avatars"

Check the document: https://developer.android.com/studio/write/tool-attributes

Cause of, You are used tools:src attribute instead of android:src .

tool:<attributes> = Working only in preview emulator. It just show you Temporary hint.

android:<attributes> = Used for the actual definition that you have defined

for Ex. If you use tool:text within textView. It's only shown like hint as how looks applied theme.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM