//xml file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/back_img"
android:background="@drawable/bg1" >
</ImageView>
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</FrameLayout>//Java file
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableLayout.LayoutParams;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;public class test extends Activity {
/** Called when the activity is first created. */
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
static View.OnTouchListener mTouchListener;
Button btn;
ImageView m_img;
TableLayout tableLayout; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.test); m_img = (ImageView)findViewById(R.id.back_img);
tableLayout = (TableLayout) findViewById(R.id.TableLayout01);
tableLayout.setStretchAllColumns(true);
int win_width=this.getWindowManager().getDefaultDisplay().getWidth();
int cell_width = win_width/8;
int win_height=this.getWindowManager().getDefaultDisplay().getHeight()- 25;
int cell_height=win_height/20; for (int row = 0; row < 20; row++) {
final TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(FP, WC));
for (int col = 0; col < 8; col++) {
btn = new Button(this);
btn.setText(""+col+"'"+row);
btn.setLayoutParams(new TableRow.LayoutParams(cell_width, cell_height));
btn.setBackgroundResource(R.drawable.icon);
btn.setOnTouchListener(this.mTouchListener);
btn.setId(col * 1000 + row);
tableRow.addView(btn);
}
//move button
mTouchListener = new OnTouchListener() {
int[] temp = new int[] {0,0};
public boolean onTouch(View v,MotionEvent event) {
int eventaction = event.getAction(); int x = (int) event.getRawX();
int y = (int) event.getRawY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
temp[0] = (int)event.getX();
temp[1] = y - v.getTop();
break;
case MotionEvent.ACTION_MOVE:
v.layout(x - temp[0],y - temp[1], x + v.getWidth() - temp[0],
y - temp[1] + v.getHeight());
//v.postInvalidate();
//v.invalidate();
tableLayout.bringChildToFront(btn);
//tableLayout.postInvalidate();
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return false;
}
};
// 新建的TableRow添加到TableLayout
tableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/back_img"
android:background="@drawable/bg1" >
</ImageView>
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</FrameLayout>//Java file
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableLayout.LayoutParams;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;public class test extends Activity {
/** Called when the activity is first created. */
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
static View.OnTouchListener mTouchListener;
Button btn;
ImageView m_img;
TableLayout tableLayout; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.test); m_img = (ImageView)findViewById(R.id.back_img);
tableLayout = (TableLayout) findViewById(R.id.TableLayout01);
tableLayout.setStretchAllColumns(true);
int win_width=this.getWindowManager().getDefaultDisplay().getWidth();
int cell_width = win_width/8;
int win_height=this.getWindowManager().getDefaultDisplay().getHeight()- 25;
int cell_height=win_height/20; for (int row = 0; row < 20; row++) {
final TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(FP, WC));
for (int col = 0; col < 8; col++) {
btn = new Button(this);
btn.setText(""+col+"'"+row);
btn.setLayoutParams(new TableRow.LayoutParams(cell_width, cell_height));
btn.setBackgroundResource(R.drawable.icon);
btn.setOnTouchListener(this.mTouchListener);
btn.setId(col * 1000 + row);
tableRow.addView(btn);
}
//move button
mTouchListener = new OnTouchListener() {
int[] temp = new int[] {0,0};
public boolean onTouch(View v,MotionEvent event) {
int eventaction = event.getAction(); int x = (int) event.getRawX();
int y = (int) event.getRawY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
temp[0] = (int)event.getX();
temp[1] = y - v.getTop();
break;
case MotionEvent.ACTION_MOVE:
v.layout(x - temp[0],y - temp[1], x + v.getWidth() - temp[0],
y - temp[1] + v.getHeight());
//v.postInvalidate();
//v.invalidate();
tableLayout.bringChildToFront(btn);
//tableLayout.postInvalidate();
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return false;
}
};
// 新建的TableRow添加到TableLayout
tableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC));
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货