Android矢量Drawable问题:首图形未生成、次图形无法占满宽度
Troubleshooting Your Android Vector Drawable Issues
Hey there! Let's work through those two vector drawable problems you're facing:
1. First Vector Drawable Not Generating at All
This usually boils down to a syntax issue in your vector XML, compatibility settings, or a wrong reference. Here are the fixes to try:
- Check your vector XML for errors: Open the first vector drawable file in Android Studio—look for red underlines in the code (like missing closing tags, invalid path
dvalues, or missing required attributes on the<vector>tag). Make sure the root<vector>has these mandatory attributes:<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24"> <!-- Your path/shape elements here --> </vector> - Enable vector compatibility: If you're targeting Android versions older than API 21, add this to your app-level
build.gradleto support vector drawables across devices:android { defaultConfig { vectorDrawables.useSupportLibrary = true } } - Verify your drawable reference: Double-check that you're using the correct name in your layout (Android is case-sensitive!). For example, if your file is
custom_shape_1.xml, make sure you're referencing@drawable/custom_shape_1and not a typo like@drawable/Custom_Shape_1.
2. Second Vector Drawable Won't Fill the Full Width
This is almost always a scaling or layout configuration issue. Try these steps:
- Adjust the vector's size attributes: In your second vector XML, set
android:width="match_parent"and keepandroid:viewportWidthset to the original vector's intrinsic width (e.g., 24 for a 24x24 vector). This lets the drawable stretch to fit its container. - Tweak your layout's View settings: If you're using an
ImageViewto display the drawable, update these attributes:<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/your_second_vector" android:scaleType="fitXY" <!-- Stretches to fill width/height, or use "fitStart" to keep aspect ratio while filling width --> android:adjustViewBounds="true" /> <!-- Helps maintain aspect ratio if needed --> - Check your LinearLayout orientation: If your parent
LinearLayoutis set toandroid:orientation="horizontal", make sure the child view holding the vector hasandroid:layout_weight="1"(if you want it to take available space) orandroid:layout_width="match_parent"(if it's the only child).
If you can share the full XML code for both vector drawables and your complete main.xml layout, we can pinpoint the exact issue even faster!
内容的提问来源于stack exchange,提问作者john
相关产品推荐
相关产品推荐

