これまでのところTextView
、FrameLayout
内に2 つを表示するアプリを作成しました。これらの のテキストとテキストに従って位置Fragment
を更新するボタンがあります。TextView
これで、テキストが正しく更新され、目的の位置が表示されますが、実際の位置とは決して一致せず、関数の最後に取得されます (以下を参照)。
関数は次のとおりです。
public void randomTextPos(View v) {
FrameLayout rootView = (FrameLayout) findViewById(R.id.fragm_root_layout);
TextView child = null;
Log.i("size", "size = " + rootView.getWidth() + " " + rootView.getHeight());
position[0] = (int) map((float) (Math.random()), 0, 1, 0,
rootView.getWidth());
position[1] = (int) map((float) (Math.random()), 0, 1, 0,
rootView.getHeight());
Log.i("onClick", "TextView at pos " + position[0] + " "
+ position[1]);
if (idChild == 0) { // choose the child textview
child = (TextView) rootView.findViewById(R.id.first_text);
} else {
child = (TextView) rootView.findViewById(R.id.second_text);
}
String mystring = Float.toString(position[0]) + "\n"
+ Float.toString(position[1]);
char[] mystringtochar = mystring.toCharArray();
child.setText(mystringtochar, 0, mystringtochar.length);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
child.getWidth(), child.getHeight());
params.gravity = Gravity.CENTER;
params.leftMargin = (int) position[0];
params.topMargin = (int) position[1];
child.setLayoutParams(params);
child.invalidate();
rootView.invalidate();
String myactualposition = "Left = " + Integer.toString(child.getLeft())
+ " Top " + Integer.toString(child.getTop());
Log.i("Position!!!", myactualposition);// here I see that the getLeft
doesn't match with position[0]
idChild = idChild == 0 ? 1 : 0;
}
Math.random() から値をマップするために使用する関数は次のとおりです。これは、既に正常に使用されているため、問題にはなりません。
public static float map(float x, float in_min, float in_max, float out_min,
float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
のレイアウトは次のFragment
とおりです。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragm_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red" >
<TextView
android:id="@+id/first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first text\n view" />
<TextView
android:id="@+id/second_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second text\n view" />
</FrameLayout>
前もって感謝します。これは単純なものであり、私には見えません。