FrameLayout 中的 Android 中心视图不起作用 [英] Android center view in FrameLayout doesn't work

查看:22
本文介绍了FrameLayout 中的 Android 中心视图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想在 FrameLayout 中居中,但我无法做到.Texview 居中就好了,当我让它可见时,我的自定义视图仍然在左侧.

<view class="com.MyView"android:id="@+id/myView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical|center_horizo​​ntal"android:visibility="gone"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical|center_horizo​​ntal"机器人:文本=居中"/></FrameLayout>

对 Mathias 来说,我不会在构造函数中做任何事情,只是很简单

 public class MyMapView extends View {私人 int xPos = 0;私人 int yPos = 0;私有位图 trackMap;私人矩阵背景矩阵;private Paint backgroundPaint;私有位图位置;私人矩阵位置矩阵;私人油漆位置油漆;公共 MyMapView(上下文上下文){超级(上下文);初始化(上下文,空);}公共 MyMapView(上下文上下文,AttributeSet attrs){超级(上下文,属性);初始化(上下文,属性);}公共 MyMapView(上下文上下文,AttributeSet attrs,int defStyle){超级(上下文,属性,defStyle);初始化(上下文,属性);}私有无效初始化(最终上下文上下文,AttributeSet attrs){背景矩阵 = 新矩阵();backgroundPaint = new Paint();backgroundPaint.setFilterBitmap(true);position = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.position);位置矩阵 = 新矩阵();positionPaint = new Paint();positionPaint.setFilterBitmap(true);}@覆盖protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));}@覆盖受保护的无效 onDraw(画布画布){int width = getMeasuredWidth();int height = getMeasuredHeight();如果(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;无效();}}

解决方案

我建议使用 RelativeLayout 而不是 FrameLayout.

假设您希望 TextView 始终位于 ImageView 下方,我将使用以下布局.

<图像视图android:id="@+id/imageview"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"机器人:layout_centerInParent =真"android:src="@drawable/icon"机器人:可见性=可见"/><文本视图android:id="@+id/textview"android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:layout_centerInParent =真"android:layout_below="@id/imageview"机器人:重力=中心"android:text="@string/hello"/></RelativeLayout>

请注意,如果您将元素的 visibility 设置为 gone,那么该元素将消耗的空间将消失,而当您使用 invisible 时相反,它将保留它消耗的空间.

如果您想在 ImageView 顶部放置 TextView,则只需省略 android:layout_alignParentTop 或将其设置为 false 并在 TextView 上省略 <代码>android:layout_below="@id/imageview" 属性.像这样.

<图像视图android:id="@+id/imageview"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="false"机器人:layout_centerInParent =真"android:src="@drawable/icon"机器人:可见性=可见"/><文本视图android:id="@+id/textview"android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:layout_centerInParent =真"机器人:重力=中心"android:text="@string/hello"/></RelativeLayout>

我希望这就是您要找的.

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

解决方案

I'd suggest a RelativeLayout instead of a FrameLayout.

Assuming that you want to have the TextView always below the ImageView I'd use following layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:src="@drawable/icon"
        android:visibility="visible"/>
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/imageview"
        android:gravity="center"
        android:text="@string/hello"/>
</RelativeLayout>

Note that if you set the visibility of an element to gone then the space that element would consume is gone whereas when you use invisible instead the space it'd consume will be preserved.

If you want to have the TextView on top of the ImageView then simply leave out the android:layout_alignParentTop or set it to false and on the TextView leave out the android:layout_below="@id/imageview" attribute. Like this.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_centerInParent="true"
        android:src="@drawable/icon"
        android:visibility="visible"/>
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="@string/hello"/>
</RelativeLayout>

I hope this is what you were looking for.

这篇关于FrameLayout 中的 Android 中心视图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆