したがって、独自のビューを作成し、それにいくつかのTextViewを追加するセットアップがあります。ただし、重力設定は壊れています。(水平方向に中央に配置されますが、垂直方向には中央に配置されません)TextViewsだけでなく、ビュー内にも描画しているものがあるため、このようにしていますが、これらは正常に機能します。TextViewの重力にのみ問題があります。これが私が持っているものの部分的なコードです。
public class myView extends View {
protected RelativeLayout baseLayout;
protected TextView textView1;
protected TextView textView2;
public myView (Context context) {
super(context);
setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));
baseLayout = new RelativeLayout(context);
baseLayout.setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));
textView1 = new TextView(context);
// initialize textView1 string, id, textsize, and color here
textView2 = new TextView(context);
// initialize textView2 string, id, textsize, and color here
baseLayout.addView(textView1);
baseLayout.addView(textView2);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Resources res = getResources();
// calculate out size and position of both textViews here
textView1.layout(left1, top1, left1 + width1, top1 + height1);
textView1.setGravity(Gravity.CENTER);
textView1.setBackgroundColor(green); // just to make sure it's drawn in the right spot
textView2.layout(left2, top2, left2 + width2, top2 + height2);
textView2.setGravity(Gravity.CENTER);
textView2.setBackgroundColor(blue); // same as above
baseLayout.draw(canvas);
}
}
これにより、TextViewが必要な正確なスポットとサイズで描画されます(背景色のためにわかります)が、重力により、垂直方向ではなく水平方向にのみ中央に配置されます。(はい、TextViewsは実際のテキスト文字列よりも大きくなります)
ここにあるソリューション(TextView重力)をおそらく実装できますが、これを回避するための非常に効率的または信頼性の高い方法とは思えません。重力が正しく機能しなくなる原因となっている、私が間違っていることはありますか?任意の入力/ヘルプをいただければ幸いです。