我正在使用tablelayout
来显示电路板。我的问题在于getY()
,始终返回0.0
,但getX()
始终返回有效值。我试图在我的视图中更改某些值,但不起作用。返回0.0
的视图的ID为b0x0
。
有什么想法吗?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator" >
<TableLayout
android:id="@+id/tableLayoutBoard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/message"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp">
<TableRow
android:id="@+id/tableRow0"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/b0x0"
android:tag="0x0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="2dp"
android:background="@drawable/box"
android:text="@string/value"
android:textStyle="bold"
android:textSize="26sp"
android:gravity="center" />
...
修改
当有人在棋盘上按下一个方框时,我会调用getY()
。我想使用getX()
和getY()
来显示键盘。
我董事会的来电者:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
iniBoard();
...
}
// initialize and set all the board's textviews
private void iniBoard() {
for (int i = 0; i < mBoardIds.length; i++) {
for (int j = 0; j < mBoardIds.length; j++) {
mBoardTextView[i][j].setOnClickListener(mBoardListener);
}
}
}
我董事会的听众:
private View.OnClickListener mBoardListener = new View.OnClickListener() {
public void onClick(View v) {
...
coord = getBoxCoordinates(v);
...
}};
// get the coordinates where should appear my keypad
private float[] getBoxCoordinates(View v) {
String[] str;
float x, y, coord[];
...
Log.d(TAG, "getX: " + v.getX() + " getY: " + v.getY());
Log.d(TAG, "height: " + v.getHeight() + " width: " + v.getWidth());
Log.d(TAG, "X: " + v.getX() + " Y: " + v.getY());
Log.d(TAG, "windows width: " + mBoardView.getWidth());
...
}