开发者

How do I inflate an XML ShapeDrawable and also define its position on screen?

I'm totally confused on drawing shapes I define in an XML file and whether I need a view, canvas or shapedrawaable etc etc!

What I'm Attempting To Achieve

I want to define several BASIC shapes in XML such as a rectangle and circle including their dimensions and colour. I would also like to define their position on screen.

My Code So Far

BasicShapes.java

package com.kerry.basicshapes;

import android.app.Activi开发者_运维技巧ty;
import android.os.Bundle;

public class BasicShapes extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
</LinearLayout>

shapes.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#f0600000"/>
    <stroke android:width="10dp" android:color="#00FF00"/>
    <corners android:radius="15dp" />
    <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
</shape>

How do I get the rectangle I've defined in shapes.xml onto the screen? I can't find any examples of what I want to do and can't get my head around the Android documentation. Can I achieve this in main.xml and how do I stipulate the size and position of the XML defined shape?

Many thanks for your help

Kerubu


Try adding the following to your main.xml in the container that you want to contain it, seems to work for me.

<ImageView
    android:src="@drawable/generic_window_shape"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

Having a similar problem, but this might have helped me too ;)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜