I have a FrameLayout in which I have 2 controls: - a custom view which draws a image and some text on it - a textview with a text
I want to center both in the FrameLayout but I can't manage to do it. The Texview is centered just fine, my cusom view remains on the left side, when I make it visible.
<FrameLayout android:id="@+id/CompassMap" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"> <view class="com.MyView" android:id="@+id/myView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:visibility="gone"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:text="CENTERED" /> </FrameLayout>
To Mathias, I don't do anything in the constructor, it's just simple
public class MyMapView extends View { private int xPos = 0; private int yPos = 0; private Bitmap trackMap; private Matrix backgroundMatrix; private Paint backgroundPaint; private Bitmap position; private Matrix positionMatrix; private Paint positionPaint; public MyMapView(Context context) { super(context); init(context, null); } public MyMapView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public MyMapView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs); } private void init(final Context context, AttributeSet attrs) { backgroundMatrix = new Matrix(); backgroundPaint = new Paint(); backgroundPaint.setFilterBitmap(true); position = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.position); positionMatrix = new Matrix(); positionPaint = new Paint(); positionPaint.setFilterBitmap(true); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)); } @Override protected void onDraw(Canvas canvas) { int width = getMeasuredWidth(); int height = getMeasuredHeight(); if (trackMap!=null) { Bitmap resizedBitmap = Bitmap.createScaledBitmap(trackMap, height, height, true); canvas.drawBitmap(resizedBitmap, backgroundMatrix, backgroundPaint); } canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.translate(xPos-position.getWidth()/2, yPos-position.getHeight()/2); canvas.drawBitmap(position, positionMatrix, positionPaint); canvas.restore(); } public void updatePosition(int xpos, int ypos, Bitmap trackImage) { xPos=xpos; yPos=ypos; trackMap = trackImage; invalidate(); } }
We can align a view in center of the FrameLayout by setting the layout_gravity of the child view.
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
To center a view, just drag the handles to all four sides of the parent.
RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets. TableLayout : is a view that groups its child views into rows and columns. FrameLayout : is a placeholder on screen that is used to display a single ...
We can align a view in center of the FrameLayout
by setting the layout_gravity
of the child view.
In XML:
android:layout_gravity="center"
In Java code:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.gravity = Gravity.CENTER;
Note: use FrameLayout.LayoutParams
not the others existing LayoutParams
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With