有没有一种简单的方法可以将Material Design图标库中的所有图标导入到Android项目中,而不必手动操作?
有没有一种简单的方法可以将Material Design图标库中的所有图标导入到Android项目中,而不必手动操作?
请查看矢量资源工具(Vector Asset Studio)
按照以下步骤开始使用矢量资源工具:
- 在Android Studio中打开一个Android应用项目。
- 在“项目”窗口中,选择“Android”视图。
- 右键单击“res”文件夹,然后选择“New > Vector Asset”。
打开矢量资源工具后,您可以按照以下方式添加材料设计图标:
- 选择“材料设计图标”(通过单击“剪贴画:ICON”)
- 点击“选择”
- 选择一个材料设计图标
这是一个脚本,用于克隆材料设计图标的GitHub仓库,网址为
https://github.com/google/material-design-icons
并创建所有文件的索引。它还将svg文件按类别复制到子目录中。您可以将其用作将感兴趣的文件复制到项目中的基础-只需修改find和cp复制语句以符合您的喜好。例如,如果您需要某个特定大小的png,则它们位于相邻的目录中,您需要相应地修改find和copy命令。#!/bin/bash
# WF 2016-06-04
# get google material design icons
# see https://dev59.com/Vl4b5IYBdhLWcg3wxUVi
tmp=/tmp/icons
index=$tmp/index.html
mkdir -p $tmp
cd $tmp
if [ ! -d material-design-icons ]
then
git clone https://github.com/google/material-design-icons
fi
cat << EOF > $index
<html>
<head>
<head>
<body>
<h1>Google Material Design Icons</h1>
EOF
for icon in `find . -name *.svg | grep production | grep 48`
do
svg=`basename $icon .svg`
category=`echo $icon | cut -f3 -d '/'`
echo $category $svg.svg
mkdir -p $tmp/$category
cp $icon $tmp/$category
echo " <img src='"$icon"' title='"$category $svg"' >" >> $index
done
cat << EOF >> $index
</body>
</html>
EOF
我觉得这个链接对我很有帮助。
https://dev.materialdesignicons.com/getting-started/android
可以使用gradle implementation。
dependencies {
implementation 'net.steamcrafted:materialiconlib:1.1.5'
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" <!-- important, you'll have to include this to use the custom xml attributes -->
xmlns:tools="http://schemas.android.com/tools" >
<!-- example of a menu item with an icon -->
<item
android:title="Disable Wifi"
app:showAsAction="always"
app:materialIcon="wifi_off" <!-- This sets the icon, HAS AUTOCOMPLETE ;) -->
app:materialIconColor="#FE0000" <!-- Sets the icon color -->
/>
</menu>
import androidx.compose.material.Icon
Icon(Icons.Rounded.Menu, contentDescription = "Localized description")
这很简单,而且非常有效!
Icons.Default.icon_name
或 Icons.Filled.icons_name
等方式引用所需的图标。
为此,您需要将扩展的材料图标依赖项添加到您的模块级 build.gradle 文件中。implementation "androidx.compose.material:material-icons-extended:$compose_ui_version"
R8和Proguard都是可以用来从您的应用的APK文件中剥离未使用的图标的工具。R8是Proguard的继任者,通常被认为在剥离未使用的代码方面更加有效。一个独立的库,androidx.compose.material:material-icons-extended,包含了完整的Material图标集。由于这个库非常庞大,请确保在将此库作为直接依赖项包含时,使用R8/Proguard来剥离未使用的图标。
buildFeatures {
minifyEnabled true
shrinkResources true
}