Change fill color on vector asset in Android Studio

后端 未结 12 1885
盖世英雄少女心
盖世英雄少女心 2020-12-02 04:09

Android Studio now supports vector assets on 21+ and will generate pngs for lower versions at compile time. I have a vector asset (from the Material Icons) that I want to c

相关标签:
12条回答
  • 2020-12-02 04:54

    Android studio now supports vectors pre-lollipop. No PNG conversion. You can still change your fill color and it will work.

    In you ImageView, use

     app:srcCompat="@drawable/ic_more_vert_24dp"
    

    In your gradle file,

     // Gradle Plugin 2.0+  
     android {  
       defaultConfig {  
         vectorDrawables.useSupportLibrary = true  
       }  
     }  
    
     compile 'com.android.support:design:23.4.0'
    
    0 讨论(0)
  • 2020-12-02 04:54

    Update: AppCompat support

    Other answers suspecting if android:tint will work on only 21+ devices only, AppCompat(v23.2.0 and above) now provides a backward compatible handling of tint attribute.

    So, the course of action would be to use AppCompatImageView and app:srcCompat(in AppCompat namespace) instead of android:src(Android namespace).

    Here is an example(AndroidX: This is androidx.appcompat.widget.AppCompatImageView ;)):

    <android.support.v7.widget.AppCompatImageView
            android:id="@+id/credits_material_icon"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:scaleType="fitCenter"
            android:tint="#ffd2ee"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:srcCompat="@drawable/ic_dollar_coin_stack" />
    

    And don't forget to enable vector drawable support in gradle:

    vectorDrawables.useSupportLibrary = true 
    
    0 讨论(0)
  • 2020-12-02 04:54

    Go to you MainActivity.java and below this code
    -> NavigationView navigationView = findViewById(R.id.nav_view);
    Add single line of code -> navigationView.setItemIconTintList(null);
    i.e. the last line of my code

    I hope this might solve your problem.

    public class MainActivity extends AppCompatActivity {
    
        private AppBarConfiguration mAppBarConfiguration;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            DrawerLayout drawer = findViewById(R.id.drawer_layout);
            NavigationView navigationView = findViewById(R.id.nav_view);
            navigationView.setItemIconTintList(null);
    
    0 讨论(0)
  • 2020-12-02 04:56

    If the vectors are not showing individually set colors using fillColor then they may be being set to a default widget parameter.

    Try adding app:itemIconTint="@color/lime" to activity_main.xml to set a default color type for the widget icons.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:itemIconTint="@color/lime"
            app:menu="@menu/activity_main_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    

    VectorDrawable @ developers.android

    0 讨论(0)
  • 2020-12-02 04:59

    Add this library to the Gradle to enable color vector drawable in old android Devices.

    compile 'com.android.support:palette-v7:26.0.0-alpha1'
    

    and re sync gradle. I think it will solve the problem.

    0 讨论(0)
  • 2020-12-02 05:01

    Currently the working soloution is android:fillColor="#FFFFFF"

    Nothing worked for me except hard coding in the vector

    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
          android:fillColor="#FFFFFF"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FFFFFF"
        android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zm-6,0C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
    

    However, fillcolor and tint might work soon. Please see this discussion for more information:

    https://code.google.com/p/android/issues/detail?id=186431

    Also the colors mighr stick in the cache so deleting app for all users might help.

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