Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to create a fixed-width drawable in XML?

I'm trying to simplify some graphics from requiring a 9-patch for each density, to just using an XML drawable. It's a fairly simple graphic:

9-patch drawable

2 Segments:

  • 8dp wide for both segments
  • Top segment is 2dp tall
  • Bottom segment fills the view
  • Right side is filled with transparency

Giving this result when set as a background image:

9-patch as list item example

It seems like this should be fairly simple to recreate as an XML drawable, and would avoid creating 5 different 9-patch graphics. However, I've looked into layer-list drawables, inset drawables, clip drawables -- they all seem to require that you know the size of the view beforehand (e.g. to keep it 2dp for an inset, you'd need to set insetRight to the width of the view - 2dp). I also tried using the shape tag's size tag, but that doesn't keep a fixed size, just a fixed proportion (so for taller views it will scale proportionally).

Am I overlooking something simple, or is this something I'd have to resort to checking programatically after layout?

like image 884
Kevin Coppock Avatar asked Jan 27 '13 18:01

Kevin Coppock


People also ask

What is XML drawable?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

How do I create a rectangle in XML?

You can create a new XML file inside the drawable folder, and add the above code, then save it as rectangle. xml. To use it inside a layout you would set the android:background attribute to the new drawable shape.

How do I add an image to a drawable XML file?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure. Step 3: Then a pop-up screen will arise like below.


1 Answers

This is not possible. This is the best solution I found. Only xml, no coding, no bitmaps, no additional views )

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

        <solid
            android:color="#ff0000" />
    </shape>
</item>
<item
    android:top="4dp">
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

        <solid
            android:color="#00ff00" />
    </shape>
</item>

<item
    android:left="10dp">
    <shape
        android:shape="rectangle">
        <solid
            android:color="#FFFFFF" />
    </shape>
</item> 
</layer-list>

enter image description here

Via code you can make your own drawble type with desired behavior. Xml drawable very limited. IMHO my suggestion is the closest to what you demanded.

like image 149
Leonidos Avatar answered Oct 22 '22 18:10

Leonidos