Adding Buttons to FingerPaint Android API Sample1
Im kind of new to Android.
I am playing around with the Android FingerPaint API Sample. I have figured out how to add buttons and functions to the Menu, but how do I get buttons on the actual screen?
Is it possible to place buttons over the drawing surface? Or would I n开发者_运维技巧eed a linear (Vertical) layout on the left, and place buttons in there. Either would be fine.
Help? Thanks.
The Android FingerPaint
sample code does not use a layout; it instead just has a subclass of View
called MyView
, and then the Activity sets its content view to be an instance of MyView
.
If you want to have more than one View
on the screen, you'll need to use some sort of layout. You could use a LinearLayout
if you want the MyView
for painting to be above, below, or to the side of the buttons; if you want the buttons to be on top of the MyView
, take a look at using a FrameLayout
or RelativeLayout
.
You can either then define the layout in XML or create it manually in code. The former is more flexible and maintainable, but there will be a few hiccups.
First, create a layout XML showing how you want your components to be laid out. For this example, we'll call it finger_paint.xml
. Make sure you have a MyView
in there somewhere, something like:
<view class="com.example.android.apis.graphics.FingerPaint$MyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Then, replace the line that looks like
setContentView(new MyView(this));
with
setContentView(R.layout.finger_paint);
Note that because MyView
does not (yet) have the proper constructor for being instantiated by the LayoutInflater
, this will not work yet, so let's fix that. Add an additional import near the top of the file:
import android.util.AttributeSet;
and then add an AttributeSet
parameter to the constructor of MyView
:
public MyView(Context c, AttributeSet as) {
super(c, as);
// rest of constructor is same as in the sample
}
You will also have to change MyView to be a static
inner class of FingerPaint
.
You may find the Building Custom Components document and the NotePad sample useful as you figure this out.
Good luck!
Are you trying to dynamically position your buttons?
If so, you can use setLayoutParams to set the layout properties of the button.
精彩评论