Android - imageView.getDrawable() returns null

前端 未结 1 527
花落未央
花落未央 2021-01-13 18:17

I am stuck on this seemingly easy issue. What am I doing wrong ?

ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState)          


        
相关标签:
1条回答
  • 2021-01-13 18:36

    You set the drawable to background attribute of 'src'.

    android:background="@drawable/clip"

    . So change yours activity_main.xml by replacing background attribute to src:

    android:src="@drawable/clip"

    Finally yours activity_main.xml will be:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
    <ImageView
        android:id="@+id/image"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:scr="@drawable/clip"
        android:layout_centerHorizontal="true"
        />
    


    Or you can get yours drawable by calling method getBackground() which returns drawable that was set by attribute android:background

    0 讨论(0)
提交回复
热议问题