gpt4 book ai didi

FrameLayout中的Android中心 View 不起作用

转载 作者:IT老高 更新时间:2023-10-28 13:12:48 27 4
gpt4 key购买 nike

我有一个 FrameLayout,其中有 2 个控件:- 在其上绘制图像和一些文本的自定义 View - 带有文本的 TextView

我想在 FrameLayout 中居中,但我无法做到。 Texview 居中很好,当我让它可见时,我的 cusom View 保持在左侧。

<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>

对于 Mathias,我在构造函数中什么也不做,很简单

   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();
}
}

最佳答案

我们可以通过设置 subview 的layout_gravity来将 View 对齐到FrameLayout的中心。

在 XML 中:

android:layout_gravity="center"

在 Java 代码中:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;

注意:使用 FrameLayout.LayoutParams 而不是其他现有的 LayoutParams

关于FrameLayout中的Android中心 View 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4051604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com