我想要一个图片视图和一个右下方的复选框。我已经能够得到一些接近的东西。但是,我的复选框未在中心对齐。如何在中心放置复选框?
这是我的代码:
// Creating a new LinearLayout
linearLayout = new LinearLayout(mContext);
// Setting the orientation to vertical
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Defining the LinearLayout layout parameters to wrap content.
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mThumbIds[position]);
imageView.setPadding(8, 8, 8, 8);
linearLayout.addView(imageView);
CheckBox checkbox = new CheckBox(mContext);
checkbox.setGravity(Gravity.CENTER);//this does not help
linearLayout.addView(checkbox);
// Creating a new LinearLayout
linearLayout = new LinearLayout(mContext);
// Setting the orientation to vertical
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Defining the LinearLayout layout parameters to wrap content.
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mThumbIds[position]);
imageView.setPadding(8, 8, 8, 8);
linearLayout.addView(imageView);
CheckBox checkbox = new CheckBox(mContext);
checkbox.setGravity(Gravity.CENTER);//this does not help
linearLayout.addView(checkbox);
答案 0 :(得分:0)
我建议您从xml文件中的LinearLayout
到另一个LinearLayout
取出复选框。或者在图片视图下方插入RelativeLayout
。
答案 1 :(得分:0)
该死!在您的情况下,RelativeLayout
比LinearLayout
更有用。
我使用RelativeLayout
实现了,请查看以下代码:
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams llp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imageparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView imageView = new ImageView(this);
//imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.drawable.testing);
imageView.setPadding(8, 8, 8, 8);
imageView.setLayoutParams(imageparam);
imageView.setId(1);
layout.addView(imageView);
RelativeLayout.LayoutParams checkparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
CheckBox checkbox = new CheckBox(this);
checkparam.addRule(RelativeLayout.BELOW, imageView.getId());
checkparam.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
checkbox.setLayoutParams(checkparam);
layout.addView(checkbox);
setContentView(layout,llp);