我创建了从ViewGroup扩展的自定义组件。我使用onDraw
作为背景,使用onLayout
作为定位视图。
但在夸大布局和定位视图之后,这些视图看起来很奇怪。 EditText
的提示是垂直的,button
的文字不居中。但是像往常一样键入文本。就像图片一样:
源代码:
public class CustomView extends LinearLayout
private Bitmap fullImage;
private int canvasWidth;
private int canvasHeight;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
initializeCanvasSize(context);
final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.favorites_layout, this, true);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final ViewGroup parentLayout = (ViewGroup) getChildAt(0);
parentLayout.layout(l, t, r, b);
final int elementCenter = canvasHeight / 3; // EditText, EditText and Button
final View name = parentLayout.getChildAt(NAME_ELEMENT);
final View url = parentLayout.getChildAt(URL_ELEMENT);
final View save = parentLayout.getChildAt(SAVE_BUTTON_ELEMENT);
name.layout(10, elementCenter / 2 - EDIT_TEXT_HEIGHT / 2, canvasWidth - 10, elementCenter / 2 + EDIT_TEXT_HEIGHT / 2);
url.layout(10, elementCenter * 2 - elementCenter / 2 - EDIT_TEXT_HEIGHT / 2, canvasWidth - 10, elementCenter * 2 - elementCenter / 2 + EDIT_TEXT_HEIGHT / 2);
save.layout(canvasWidth - 220, elementCenter * 3 - elementCenter / 2 - BUTTON_HEIGHT / 2, canvasWidth - 10, elementCenter * 3 - elementCenter / 2 + BUTTON_HEIGHT / 2);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int desiredWSpec = MeasureSpec.makeMeasureSpec(canvasWidth, MeasureSpec.AT_MOST);
final int desiredHSpec = MeasureSpec.makeMeasureSpec(canvasHeight, MeasureSpec.AT_MOST);
this.setMeasuredDimension(desiredWSpec, desiredHSpec);
}
@Override
protected void onDraw(Canvas cvs) {
if (fullImage == null) {
fullImage = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(fullImage);
paint.reset();
paint.setColor(Color.parseColor("#CC000000"));
canvas.drawRect(0, 0, canvasWidth, canvasHeight, paint);
}
cvs.drawBitmap(fullImage, 0, 0, null);
}
}
favorites_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:background="@drawable/search_url_selector"
android:singleLine="true"
android:inputType="text"
android:paddingLeft="5dp"
android:hint="@string/favorite_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:background="@drawable/search_url_selector"
android:singleLine="true"
android:inputType="text"
android:paddingLeft="5dp"
android:hint="@string/favorite_url"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:background="@drawable/save_favorites_button_selector"
android:text="@string/favorite_save"/>
</FrameLayout>
我认为这可能是因为选择器错了。但是当我删除它时结果是一样的。
请问您应该建议我至少应该在哪个区域找到正确的答案?