2

幅と高さの両方を持つLinearLayout(ホルダー)があります。match_parentこのホルダーにView(拡張するクラスView) を挿入します。onCreateアクティビティのメソッド内でこれを行います。

私の質問は、この子をView水平方向と垂直方向に最大に伸ばし、正方形を維持して、親 LinearLayout からできるだけ多くの領域を埋めるにはどうすればよいですか?

出発点として私が現在使用しているものは次のとおりです。

    pieContainer = (LinearLayout) findViewById(R.id.pie_container_id);
    pie = new PieView(this);
    pieContainer.addView(pie);

両側 (メイン アクティビティとPieViewクラス) で onMeasure メソッドを上書きして遊んでみましたが、役に立ちませんでした。

4

2 に答える 2

1

このカスタム SquareView を試してください

public class SquareView extends View {
  public SquareView(Context context) {
    super(context);
  }

  public SquareView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public SquareView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
    setMeasuredDimension(size, size);
  }
}
于 2012-10-12T17:24:42.317 に答える
0

画面全体の幅がわかると思います

DisplayMetrics display = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics( display );
int screenW = display.widthPixels;

toの幅とViewtofill_parentの高さを設定ViewしますscreenW

多分次のようなものです:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, screenW); // (width, height)
yourView.setLayoutParams(params);

または、私はあなたの質問を完全に誤解したかもしれません。ここは遅くて疲れています:)もしそうならごめんなさい。

それがうまくいったかどうか教えてください:)

于 2012-10-12T17:17:51.367 に答える