1

我想在它下面有一个图像视图和一个复选框。我已经能够得到一些接近。但是,我的复选框没有在中心对齐。如何将复选框放置在中心?这是我的代码:

        // 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);

4

2 回答 2

0

我建议您在 xml 文件中将您的复选框从该复选框LinearLayout移到另一个复选框。LinearLayoutRelativeLayout在您的图像视图下方插入一个。

于 2013-02-05T06:28:06.277 回答
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);
于 2013-02-05T07:15:13.810 回答