It's not hard - just lots of maths....
Android gives some vague documentation here:
http://developer.android.com/guide/topics/ui/custom-components.html
I've provided a sample class that does its own layout - it's not pretty, but
should give you an idea of what's involved. I've no idea how you'd create a
generic layout that you can use in your xml or eclipse layout editor - sorry.
Hopefully someone else can provide a few tips on how to do this.
The following class contains 3 child objects - a button, a text view & an edit
text. I probably could have used a relative layout for this, but I wanted to
do a few other things too (that I've removed to keep this example simple).
Here I specifically wanted the component to be limited to the height of the
button (as thin as possible) and the remaining space split evenly between the
textView and the editView, with appropriate padding in between.
Note the following:
In onMeasure - I call measure on all of the child components, and call
setMeasuredDimension at the end. onMeasure can be called in a number of modes
and can definitely get quite complex. :( Read the following
In onLayout - I call layout on all of the children. You should really use the
child dimensions calculated in the last call of onMeasure by calling
[child].getMeasuredHeight()/getMeasuredWidth() but I didn't as the values
shouldn't have changed.
RequestLayout is a call that recursively travels up the component heirachy
until it reaches the ViewRoot and triggers a relayout of the entire screen.
EditTexts and other components like to call this when their contents change so
that they can grow and cause layouts as you type. If this is annoying, you can
override it to prevent this from happening.
Hope this helps rather than confuses,
Peter.
public class DataViewController extends ViewGroup
{
TextView textView;
ApcEditText editText;
Button ddButton;
private static final int BUTTON_PAD = 2;
public DataViewController(Context context, TableComponent tc)
{
super(context);
textView = new TextView(context);
textView.setText("Text");
addView(textView);
ddButton = new Button(context);
addView(ddButton);
editText = new ApcEditText(context, null, true);
addView(editText);
}
@Override
public void requestLayout()
{
// Prevent changes in the child components from causing the layout
// to be recalculated. (You don't necessarily have to overwrite
this)
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int hSize = MeasureSpec.getSize(heightMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
ddButton.measure(0, MeasureSpec.makeMeasureSpec(hMode, hSize -
getPaddingLeft() - getPaddingRight()));
int h = ddButton.getMeasuredHeight() + getPaddingTop() +
getPaddingBottom();
int minWidth = ddButton.getMeasuredWidth() + 2 * BUTTON_PAD +
getPaddingLeft() + getPaddingRight();
int wSize = MeasureSpec.getSize(widthMeasureSpec);
int w = (wSize - minWidth) / 2;
textView.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY,
w),
MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY, h));
editText.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY,
w),
MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY, h));
setMeasuredDimension(wSize, h);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
int left = getPaddingLeft();
int top = getPaddingTop();
int right = r - l - getPaddingRight();
int bottom = b - t - getPaddingBottom();
int minWidth = ddButton.getMeasuredWidth() + 2 * BUTTON_PAD +
getPaddingLeft() + getPaddingRight();
int w = (r - l - minWidth) / 2;
textView.layout(left, top, left + w, bottom);
editText.layout(left + w + BUTTON_PAD, top, left + BUTTON_PAD + w *
2, bottom);
ddButton.layout(right - ddButton.getMeasuredWidth(), top, right,
bottom);
}
}
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Martin
Sent: Wednesday, 22 September 2010 3:56 AM
To: Android Developers
Subject: [android-developers] Re: Exact Layout that fits a background image
Woow this sounds very hard. Is it explained somewhere how to do this,
or is there an example?
If I create my own layout-class, can I still use the Layout-Editor
with eclipse?
Greetings, Martin
On 21 Sep., 02:10, Peter Carpenter
<[email protected]> wrote:
> You can create your own class that extends ViewGroup and override the
> layout/onMeasure functions to explicitly define your own layout.
> Not sure of how to do this via xml though.
>
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Martin
> Sent: Tuesday, 21 September 2010 8:02 AM
> To: Android Developers
> Subject: [android-developers] Exact Layout that fits a background image
>
> Hi!
>
> I have a background image (full screen) and want to create an exact
> layout, which fits this background-image. The buttons and labels have
> to be at an exact place of the background-image. How can I do this?
> The absolute-layout is deprecated. If I use relative layout, I can use
> margins, but the positions differ from device to device.
>
> The best thing is if I could set the exact positions as percent of the
> total view size. How can I do this?
>
> Greetings, Martin
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group
> athttp://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en