android中在gridview中加入一定数量的imagebutton
2个回答
展开全部
public class SettingCommentBcakGroundActivity extends Activity {
public static int[] ImageDatas = { R.drawable.l_1, R.drawable.l_2, R.drawable.l_3,
R.drawable.l_4, R.drawable.l_5, R.drawable.l_6, R.drawable.l_7,
R.drawable.l_8, R.drawable.l_9,R.drawable.l_10,R.drawable.l_11 };
private GridView mGridView;
int Width;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//隐藏窗体标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.setting_comment_bg);
mGridView = (GridView) findViewById(R.id.id_gvw_comment_bg);
Width = getWindow().getWindowManager().getDefaultDisplay().getWidth();
mGridView.setAdapter(new AdapterGridView());
System.out.println("gridViewWidth=" + Width + "ColumnWidth=" + Width
/ 4);
}
public void onAction(View v) {
Intent intent=new Intent();
intent.putExtra(ReleaseActivity.RESULT_COMMENT_BG_NUM, 4);
setResult(RESULT_OK, intent);
this.finish();
}
class AdapterGridView extends BaseAdapter {
public int getCount() {
// TODO Auto-generated method stub
return ImageDatas.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return ImageDatas[position];
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = LayoutInflater.from(
SettingCommentBcakGroundActivity.this).inflate(
R.layout.comment_bg_item, null);
ImageView mImageView = (ImageView) convertView
.findViewById(R.id.id_img_comment_bg_item);
mImageView.setBackgroundResource(ImageDatas[position]);
LinearLayout.LayoutParams mParams=(LayoutParams) mImageView.getLayoutParams();
mParams.weight=(Width / 4)-(3*10);
mParams.height=(Width / 4)-(3*10);
mImageView.setLayoutParams(mParams);
}
return convertView;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询