自定义视图会混淆周围视图的布局

时间:2016-04-30 07:18:00

标签: android android-layout

我无法正确布局自定义视图 视图非常简单,但覆盖onMeasure始终为正方形,即高度和宽度应始终相同。

我的问题是,任何周围的视图都会混淆,并且无法正确调整大小。

预期结果:
Expected result

这是使用LinearLayout的结果:
Using LinearLayout

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_height="match_parent"
  android:layout_width="match_parent">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1" />
    <example.Square
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:color="@color/black" />
  </LinearLayout>
</LinearLayout>

这是使用RelativeLayout的结果:
enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_height="match_parent"
  android:layout_width="match_parent">
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
      android:id="+@id/edittext1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
    <example.Square
      android:layout_toRightOf="@id/edittext1"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:color="@color/black" />
  </RelativeLayout>
</LinearLayout>

以下是自定义视图的代码:

public class Square extends View {

  private Paint paint;

  protected void init() {
    paint = new Paint( Paint.ANTI_ALIAS_FLAG );
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(getResources().getColor( android.R.color.black));
  }

  public Square( Context context) {
    super(context);
    init();
  }

  public Square( Context context, AttributeSet attributeSet) {
    super(context,attributeSet);
    init();
  }

  public int getColor() {
    return paint.getColor();
  }

  public void setColor(int color) {
    paint.setColor(color);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int l = Math.min(getMeasuredHeight(),getMeasuredWidth());
    setMeasuredDimension(l,l);
  }

  @Override
  protected void onDraw( Canvas canvas ) {
    super.onDraw(canvas);
    if ( (getWidth() > 0) && (getHeight() > 0) ) {
      RectF r = new RectF( getPaddingLeft(), getPaddingTop(), getWidth()-getPaddingRight(), getHeight()-getPaddingBottom() );
      canvas.drawRect( r, paint );
    }
  }
}

1 个答案:

答案 0 :(得分:0)

当您使用RelativeLayout时,您告诉EditText尽可能多地使用android:layout_width="match_parent",然后在右侧显示您的方块。我认为您应该更改布局规则以显示方块,然后使用EditText填充可用位置。我觉得这样的事情会起作用:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="+@id/edittext1"
            android:layout_toLeftOf="@id/square"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <example.Square
            android:id="+@id/square"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:color="@color/black" />

    </RelativeLayout>

</LinearLayout>