我正在使用自定义视图制作 Android 应用程序。我以前做过很多次,但这次我似乎遇到了一个奇怪的问题。
尝试通过 XML 或布局编辑器进行布局时,我的自定义视图未按预期布局。当我将其中一个视图放在现有视图的右侧时,它似乎与左侧对齐。我附上了一张图片来说明我的意思
我不确定是什么原因造成的,任何其他视图似乎都不会发生这种情况,并且以前的应用程序从未发生过这种情况,尽管我为自定义视图使用了类似的代码。
有没有人遇到过这样的问题?如果是这样,有什么建议吗?
如果需要,我可以提供视图的代码和其他可能相关的代码。
编辑 - 好的,按照要求进行编码,希望它不会冒犯任何人太多。
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.offthemap.sudokusolver.SudokuButton
android:id="@+id/sudokuButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<com.offthemap.sudokusolver.SudokuButton
android:id="@+id/sudokuButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/sudokuButton2" />
</RelativeLayout>
自定义视图:
package com.offthemap.sudokusolver;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class SudokuButton extends View
{
private static Bitmap background, border, borderSelected;
private static Bitmap[] number;
private static int size = 100;
private int numberIndex = -1;
public SudokuButton(Context context, AttributeSet attrs)
{
super(context);
Resources res = getResources();
if (background == null){background = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.background), size, size, false);}
if (border == null){border = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.border), size, size, false);}
if (borderSelected == null){borderSelected = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.borderselected), size, size, false);}
if (number == null)
{
number = new Bitmap[]{
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.one), size, size, false),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.two), size, size, false),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.three), size, size, false),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.four), size, size, false),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.five), size, size, false),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.six), size, size, false),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.seven), size, size, false),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.eight), size, size, false),
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.nine), size, size, false)
};
}
}
public void increaseNumber()
{
numberIndex++;
if (numberIndex == 9){numberIndex = -1;}
invalidate();
}
public void setNumber(int newNumber)
{
numberIndex = newNumber;
}
public static void setSize(int newSize)
{
size = newSize;
}
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(background, 0, 0, null);
if (numberIndex != -1)
{
canvas.drawBitmap(number[numberIndex], 0, 0, null);
}
canvas.drawBitmap(border, 0, 0, null);
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec)
{
int preferred = background.getWidth();
return getMeasurement(measureSpec, preferred);
}
private int measureHeight(int measureSpec)
{
int preferred = background.getHeight();
return getMeasurement(measureSpec, preferred);
}
private int getMeasurement(int measureSpec, int preferred)
{
int specSize = MeasureSpec.getSize(measureSpec);
int measurement = 0;
switch(MeasureSpec.getMode(measureSpec))
{
case MeasureSpec.EXACTLY:
measurement = specSize;
break;
case MeasureSpec.AT_MOST:
measurement = Math.min(preferred, specSize);
break;
default:
measurement = preferred;
break;
}
return measurement;
}
}
第二次编辑 - 似乎这只是使用静态位图的一个问题,不知道为什么会发生,但删除它似乎可以解决它。