【问题标题】:Position checkbox dynamically inside linear layout android在线性布局android中动态定位复选框
【发布时间】:2013-02-05 05:39:39
【问题描述】:

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

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

【问题讨论】:

  • 添加您的屏幕截图。因为 checkbox.setGravity(Gravity.CENTER);将文本定位在中心。
  • 尝试让您的线性布局居中。LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.CENTER);

标签: android android-widget android-linearlayout android-relativelayout android-checkbox


【解决方案1】:

该死!在您的情况下,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);

【讨论】:

    【解决方案2】:

    我建议您在 xml 文件中将复选框从 LinearLayout 取出到另一个 LinearLayout。或者在您的图像视图下方插入RelativeLayout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 2011-10-25
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多