あらゆる画面解像度のデバイスで動作するアプリケーションを設計したいと考えています。
そうする私の考えは、使用するビューのタイプごとに 1 つのカスタム ビューを作成し、XMLlayout_width
でlayout_height
指定された と を親サイズのパーセンテージとして使用することでした (たとえば、layout_width="100dp"
は と同等fill_parent
であり、layout_width="50dp"
のView
幅は親の半分)。
私が作成したカスタムTextView
クラスは次のとおりです。
public class RSTextView extends TextView {
public RSTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int w, int h) {
int parentWidth = ((View)this.getParent()).getWidth();
int parentHeight = ((View)this.getParent()).getHeight();
Log.i(null, "Width: " + w + "; Height: " + parentHeight);
this.setMeasuredDimension(parentWidth * MeasureSpec.getSize(w) / 100,
parentHeight * MeasureSpec.getSize(h) / 100);
}
}
しかし、うまくいきません。とサイズparentWidth
はparentHeight
常に 0 です。
相対的なサイズのアプリケーションを作成するより良い方法はありますか?
それが正しい方法である場合View
、メソッドから親のサイズを取得するにはどうすればよいonMeasure
ですか?