【问题标题】:Dynamically aligning checkbox with relativelayout使用相对布局动态对齐复选框
【发布时间】:2017-05-20 16:43:32
【问题描述】:

我是 android 开发新手,在我的项目中,我想使用相对布局水平对齐复选框。我知道,使用线性布局更容易。

就我而言,复选框粘贴在同一位置

Main = (RelativeLayout) dialogLayout.findViewById(R.id.linearr);

                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                LinkedHashMap<String, String> alphabet = new LinkedHashMap<String, String>();
                alphabet.put("1", "A");
                alphabet.put("2", "B");
                alphabet.put("3", "C");
                alphabet.put("4", "D");
                alphabet.put("5", "A");
                alphabet.put("6", "B");
                alphabet.put("7", "C");
                alphabet.put("8", "D");

                Set<?> set = alphabet.entrySet(); // Get an iterator

                Iterator<?> i = set.iterator(); // Display elements
                while (i.hasNext()) {
                    @SuppressWarnings("rawtypes")


                    Map.Entry me = (Map.Entry) i.next();

                    checkBox = new CheckBox(getContext());
                    checkBox.setId(Integer.parseInt(me.getKey().toString()));
                    checkBox.setText(me.getValue().toString());
                    params.rightMargin = 30;
                    if(Integer.parseInt(me.getKey().toString())==1)
                        params.addRule(RelativeLayout.ALIGN_LEFT);
                    else
                        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,Integer.parseInt(me.getKey().toString())-1);

                    checkBox.setLayoutParams(params);

                   Main.addView(checkBox, params);

` 我们将不胜感激。

【问题讨论】:

    标签: android android-layout checkbox


    【解决方案1】:

    你可以使用

     List<String> name = new ArrayList<>();
        List<Integer> id = new ArrayList<>();
    
        for (int i = 0; i < 8; i++) {
            name.add("C" + (i + 1));
            id.add(generateViewId());
        }
    
        for (int i = 0; i < name.size(); i++) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    
            CheckBox checkBox = new CheckBox(this);
            checkBox.setId(id.get(i));
            checkBox.setText(name.get(i));
    
            if (i != 0) {
                params.addRule(RelativeLayout.RIGHT_OF, id.get(i-1));
            }
    
            checkBox.setLayoutParams(params);
            container.addView(checkBox, params);
    
        }
    

    生成唯一的 ID - generate unique ids

    /**
     * params
     * Generate a value suitable for use in
     * This value will not collide with ID values generated at build time by aapt for R.id.
     *
     * @return a generated ID value
     */
    public static int generateViewId() {
        for (; ; ) {
            final int result = sNextGeneratedId.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }
    

    输出:

    【讨论】:

      【解决方案2】:

      我做到了..诀窍是,我们要创建一个新的 params 实例..` Map.Entry me = (Map.Entry) i.next();

              RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      
              checkBox = new CheckBox(getApplicationContext());
              checkBox.setId(Integer.parseInt(me.getKey().toString()));
              checkBox.setText(me.getValue().toString());
              params.rightMargin = 30;
              if(Integer.parseInt(me.getKey().toString())==1)
                  params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
              else {
                  params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);//I added this one
                  params.addRule(RelativeLayout.RIGHT_OF, Integer.parseInt(me.getKey().toString()) - 1);
                  params.rightMargin = 30;
              }
              checkBox.setLayoutParams(params);
      
              Main.addView(checkBox, params);`
      

      【讨论】:

        猜你喜欢
        • 2011-09-24
        • 1970-01-01
        • 2012-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-19
        • 1970-01-01
        相关资源
        最近更新 更多