Android 矢量绘制应用程序: srcCompat 不显示图像

我正在使用支持库在 android kitkat 上显示矢量图像。当我在模拟器上测试我的应用程序时,我没有看到任何这些图像。我做了一个单独的布局为 Android Lollipop 和以上,它工作得很好(我认为,因为我使用的是 src属性而不是 srcCompat这里的代码,我是用户支持库

<LinearLayout android:layout_alignParentBottom="true"
android:id="@+id/lake_detail"
android:background="@drawable/my_fishing_plan_footer_line"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="90dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<RelativeLayout
android:layout_marginRight="3dp"
android:id="@+id/fire_logo"
android:layout_width="20sp"
android:layout_height="20sp">


<ImageView
android:tint="#d74313"
app:srcCompat="@drawable/circle_icon"
android:layout_width="30sp"
android:layout_height="30sp" />


<ImageView
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
app:srcCompat="@drawable/lauzaviete"
android:layout_width="25dp"
android:layout_height="25dp" />


</RelativeLayout>

这很奇怪,因为我在 android 工作室的预览窗口中看到了这些图像。

51550 次浏览

Original Answer

Use android.support.v7.widget.AppCompatImageView instead of ImageView in your layout, like this:

<LinearLayout
...
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<android.support.v7.widget.AppCompatImageView
android:tint="#d74313"
app:srcCompat="@drawable/circle_icon"
android:layout_width="30sp"
android:layout_height="30sp" />


<android.support.v7.widget.AppCompatImageView
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
app:srcCompat="@drawable/lauzaviete"
android:layout_width="25dp"
android:layout_height="25dp" />
</LinearLayout>

See the AppCompatImageView docs here and app:srcCompat here.

Also, make sure to do the following:

Setup your build.gradle

android {
defaultConfig {
vectorDrawables {
useSupportLibrary = true
}
}
}

Docs: https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.VectorDrawablesOptions.html#com.android.build.gradle.internal.dsl.VectorDrawablesOptions:useSupportLibrary

Extend your Activity with AppCompatActivity

public final class MainActivity extends AppCompatActivity {
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);
}
}

When using app:srcCompat, make sure to have the correct declarations in your layout:

<LinearLayout
...
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
...
</LinearLayout>

Optional (warning: please read docs): setCompatVectorFromResourcesEnabled in your Application class

public class App extends Application {


@Override public void onCreate() {
super.onCreate();


// Make sure we use vector drawables
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
}

Docs: https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#setCompatVectorFromResourcesEnabled(boolean)

I had a similar issue and after following all steps from Jared Burrows's answer the problem was not solved.

Turns out that the "app" namespace inside my layout file was set as:

xmlns:app="http://schemas.android.com/tools"

instead of:

xmlns:app="http://schemas.android.com/apk/res-auto"

After changing this the problem was fixed

use:

android:background="@drawable/circle_icon"

instead of:

app:srcCompat="@drawable/circle_icon"

Additional be sure that your vector drawables located in drawable and not in drawable-anydpi.

I end up often with problems if the graphics located in drawable-anydpi folder.

Implement and app:srcCompact and then you can use it on the ImageView

implementation 'com.android.support:appcompat-v7:28.0.0'

Make sure you implement the right version.

Then in your build.gradle set android.defaultConfig.vectorDrawables.useSupportLibrary = true

defaultConfig {


//...


vectorDrawables {
useSupportLibrary true
}
}

In summary:

  1. Put vector drawable support in your module (build.gradle)

    defaultConfig {
    vectorDrawables.useSupportLibrary = true
    }
    
  2. Instead of android:src="@drawable/icon" use app:srcCompat="@drawable/icon"

  3. Make sure your Activity extends AppCompatActivity without this step is not possible to show vector image with app:srcCompat

Incase anyone else runs into this problem and is using androidx, then try using androidx.appcompat.widget.AppCompatImageView

The problem was with my Code as well. Solution: As Android is upgrading to Androidx Artifacts, I used Instead of Regular and it Worked!