2
我想设置一个网格视图,显示图像按钮的网格。我还设置了一个拖动图层,以便您可以单击并拖动按钮到不同的位置。我的问题是网格视图不会显示图像按钮。 以下是设置布局的两个xml文件。 main.xml和row_grid.xml在res/layout中。Gridview将不会显示图像按钮
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<edu.purdue.app.DragLayer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drag_layer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="@integer/num_columns"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="0.8"
android:background="@color/light_gray"
/>
</edu.purdue.app.DragLayer>
row_grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageButton
android:id="@+id/MapButton"
android:layout_width="104dp"
android:layout_height="92dp"
android:gravity="center_horizontal|bottom"
android:src="@drawable/ic_launcher" >
</ImageButton>
</LinearLayout>
主要活动:
public class PurdueAppActivity extends Activity implements View.OnLongClickListener, View.OnClickListener,
View.OnTouchListener{
public static final boolean Debugging = false;
DragController mDragController; // Object that handles a drag-drop sequence. It intersacts with DragSource and DropTarget objects.
DragLayer mDragLayer; // The ViewGroup within which an object can be dragged.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView) findViewById(R.id.button_grid_view);
gridView.setAdapter (new ImageButtonCellAdapter(this,R.layout.row_grid));
// gridView.setOnItemClickListener (this);
mDragController = new DragController(this);
mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
mDragLayer.setDragController (mDragController);
mDragLayer.setGridView (gridView);
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return startDrag (v);
}
public boolean startDrag (View v)
{
DragSource dragSource = (DragSource) v;
// We are starting a drag. Let the DragController handle it.
mDragController.startDrag (v, dragSource, dragSource, DragController.DRAG_ACTION_MOVE);
return true;
}
}
最后,这里是作为适配器类getView()方法应该设置它:
public View getView (int position, View convertView, ViewGroup parent)
{
mParentView = parent;
ImageButtonCell v = null;
if (convertView == null) {
// If it's not recycled, create a new ImageCell.
v = new ImageButtonCell (mContext);
v.setLayoutParams(new GridView.LayoutParams(85, 85));
v.setScaleType(ImageView.ScaleType.CENTER_CROP);
v.setPadding(8, 8, 8, 8);
} else {
v = (ImageButtonCell) convertView;
}
v.mCellNumber = position;
v.mGrid = (GridView) mParentView;
v.mEmpty = true;
// v.setBackgroundResource (R.color.drop_target_enabled);
v.setBackgroundResource (R.color.light_gray);
//v.mGrid.requestDisallowInterceptTouchEvent (true);
//v.setImageResource (R.drawable.hello);
// Set up to relay events to the activity.
// The activity decides which events trigger drag operations.
// Activities like the Android Launcher require a long click to get a drag operation started.
v.setOnTouchListener ((View.OnTouchListener) mContext);
v.setOnClickListener ((View.OnClickListener) mContext);
v.setOnLongClickListener ((View.OnLongClickListener) mContext);
return v;
}
如果我想添加多个按钮,我假设我可以添加他们使用相同的策略为您发布的MapButton正确吗?网格视图会将它们全部放置在其布局中? – Otto45