简体   繁体   中英

Android drawable shape : make Rectangle into a Square

I'm trying to define a nice rounded corner square shape in Android, but I can only get a nice rounded corner rectangle.

My xml in /res/drawable looks like this:

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/uk.co.alexlydiate.sequencer">

<item android:state_pressed="true" 
      app:state_playing = "true" >
    <shape>
        <stroke
            android:width="2dp"
            android:color="@color/red" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
        <solid 
            android:color="@color/blue" />
        <size
            android:width="3dp"
            android:height="3dp" />
    </shape>        
</item>
</selector>

And all works fine except

<size android:width="3dp" android:height="3dp" />

Which I'd have hoped made the thing square, but no such luck.

My layout looks like this:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid8x8"
    android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

<uk.co.alexlydiate.sequencer.Key  android:background="@drawable/key"        
    android:id="@+id/keyA1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_weight="1"  android:layout_margin="2px"
/>
<uk.co.alexlydiate.sequencer.Key  android:background="@drawable/key"        
    android:id="@+id/keyA2"
    android:layout_below="@+id/keyA1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_weight="1"  android:layout_margin="2px"
/>
</RelativeLayout>

Anybody got any bright ideas? Thanks!

AFAIK: shapes stretch themselves according to the View 's layout. Instead you can define the Square View and set shape background.

OR

Use an Image as background

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM