开发者

How to add XML drawable in Eclipse

Ok guys I'm little stacked here. According to official documentation Google says that "Once you've defined your Drawable in XML, save the file in the res/drawable/ directo开发者_如何转开发ry of your project." is the way to add a XML drawable to the project.

But when I created the project ADT created 3 different drawable folders for mdpi, hdpi, and ldpi. So when I wanted to create my XML drawable, I right click on my drawable-hdpi folder and select "Add new Android XML file", and there I have to select what kind of XML file I want to create, but there isn't "drawable" to select.

If I create new text file and save it as button_drawable.xml Eclipse says that there is an Error in the file and that It can't build my project.

So, please tell me how do you add xml drawables in Eclipse ADT?


Just create a drawable folder by yourself and put your files there.


I have that problem sometimes.

I always create the XML drawable as "New text file". If you get an error after the XML drawable has been created, check the XML syntax to be sure there is no error in the file, and try to clean the project (Project->Clean->Your project)

Good luck


As of today, there is no "direct" support to drawable resources in Eclipse ADT.

So, from Eclipse Package Explorer, you should "manually" add a new folder named 'drawable' into your project 'res' folder.

Then, still from Package Explorer, you should manually add a new XML file within the newly created folder (right click\ new\ other\ XML\ XML File).


Why cant you use BitmapDrawable heres a sample code:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/drawable_resource"
android:gravity="center"
android:tileMode="repeat"/>
  1. Just replace with your drawable src name and save this as .xml file.
  2. Store the file in res/drawable directory.
  3. Assign the reference to android:background="@drawable/drawable_resource" and it will be tiled.

Example:

<LinearLayout
android:background="@drawable/drawable_resource"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LineatLayout

Heres a complete template of Bitmap Drawable, you can choose attribute values accourding to your need.

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:antialias=["true" | "false"]
    android:dither=["true" | "false"]
    android:filter=["true" | "false"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                      "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                      "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />

You can find documentation of the same here look for XML Bitmap.


Just add a new XML file into the res/drawable folder. The drawable file looks like next:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#FF000000"
        android:centerColor="#FF000000"
        android:endColor="#FF777777"
        android:angle="90" />
</shape>


Drag and drop an image-button from "Images&Media" folder in palette. When the dialog for choosing an icon is shown, klick on "create a new icon" down below, enter a name for your new drawable (only small letters) and then choose your image and what the drawable is for (a button or what so ever). Remember to choose the right THEME, that you are creating the icon for (a light theme or a dark theme). Eclipse will resize the pic and do all the rest work for you to have good-looking drawable. After that, you can delete the image-button. It's a kind of strange, but it works that way the fastest and WITHOUT CODING and without any graphical working or any filemanagement!!!!


It's is exactly like you said : you have the create the file manually. But also the XML file content has to be valid. That's all. You can show us your button_drawable.xml content so we check it.


This worked for me in creating a "drawable" folder inside the res folder. Right click on the res folder->New->Other->General->Folder then Next

Select res from the set of folders under your project then

give it the "drawable" name and Finish!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜