以编程方式对齐LinearLayout上的视图

时间:2013-05-16 13:38:28

标签: android json android-layout user-interface android-linearlayout

我在解析json后正在开发一个ListView UI,我们正在寻找一个基于模板的解决方案来列出行,其中每个列表行可能有不同的2字段,它们的对齐方式将由服务器以json的形式驱动。 我已经解析了我的JSON并为ListView开发了行,如下面的

enter image description here

但我无法在linearLayout中调整我的观点, 比如Name应该在大多数左边,其余的视图应该是右对齐的,例如我们通过XML UI完成的。

enter image description here

任何人都可以帮我在LinearLayout的上部listView中对齐这些viwes就像我的XML视图一样。

我正在添加几个水平方向的linearLayout并在这些布局中添加所有子视图,添加所有子项后,我将此布局添加到另一个垂直方向的LinearLayout中。

我已经尝试过了

在LinearLayout中添加视图时传递RelativeLayout和LinearLayout限定参数以进行对齐,但它不起作用,我可以从左侧或右侧添加视图。

这是我的代码,我尝试添加视图以为List创建一行。

public class RowLayout extends LinearLayout{
    Context context;
Hashtable<String, LinearLayout> ht = new Hashtable<String, LinearLayout>();
LinearLayout ll;
WorkkardData workkardData=null;
public RowLayout(Context context) {
    super(context);
    this.context=context;
}

public LinearLayout generateLayout(Object object, WorkkardData workkard){
    this.workkardData=workkard;
    JSONObject json = (JSONObject)object;
    String template="";
    String cardBackground="";
    String cardSelectedBackground="";
    String fieldName="";
    try {
        template = json.getString("templateId");
    } catch (Exception e) {
    }
    try {
        cardBackground = json.getString("backgroundColor");
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        cardSelectedBackground = json.getString("selectedBackgroundColor");
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        cardBackground = json.getString("backgroundColor");
    } catch (Exception e) {
        e.printStackTrace();
    }

    LinearLayout workkardrowLayout = new LinearLayout(context);
    workkardrowLayout.setOrientation(LinearLayout.VERTICAL);
    workkardrowLayout.setBackgroundColor(Color.parseColor(cardBackground));

    RoundRectShape roundRect = new RoundRectShape(new float[]{10,10,10,10,10,10,10,10}, null, null);

    GradientDrawable gd = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.parseColor(cardBackground), Color.parseColor("#F0F5F4")});
    gd.setStroke(1, Color.parseColor("#b4bdbf"));
    workkardrowLayout.setBackgroundDrawable(gd);

    try {
        JSONArray childrentop = json.getJSONArray("children");
        for (int i = 0; i < childrentop.length(); i++) {
            JSONArray subChildren = childrentop.getJSONObject(i).getJSONArray("children");

            String width="350", height="0", rightAlignmen="0";
            int widthInt=0;
            try {
                width = childrentop.getJSONObject(i).getString("width");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                height = childrentop.getJSONObject(i).getString("height");
                System.out.println("height::::::::::::::::::::::::::::"+height);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                rightAlignmen = childrentop.getJSONObject(i).getString("rightAlign");
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(width.equalsIgnoreCase("100%")){
                widthInt = 350;
            }else{
                widthInt = 350*(Integer.parseInt(width.trim().split("%")[0]))/100;
            }

            RelativeLayout.LayoutParams paramsll1 = new RelativeLayout.LayoutParams(widthInt,Integer.parseInt(height.trim()));
            ll = new LinearLayout(context);
            if(subChildren.length()==0){
                ht.put("i", ll);
                workkardrowLayout.addView((View) ht.get("i"));
            }else{
                TextView[] textView = new TextView[3];
                for (int j = 0; j < subChildren.length(); j++) {

                    String type = "", field= "", backgroundColor ="", textColor ="", alignment ="",horizontalPadding ="", bold="",widthChild="",textColorChild="",fontSize="",style="lower";
                    try {
                        type = subChildren.getJSONObject(j).getString("type");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        field = subChildren.getJSONObject(j).getString("field");
                        fieldName=field;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        backgroundColor = subChildren.getJSONObject(j).getString("backgroundColor");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        textColor = subChildren.getJSONObject(j).getString("textColor");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        alignment = subChildren.getJSONObject(j).getString("alignment");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        horizontalPadding = subChildren.getJSONObject(j).getString("horizontalPadding");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        bold = subChildren.getJSONObject(j).getString("bold");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        widthChild = subChildren.getJSONObject(j).getString("width");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        textColorChild = subChildren.getJSONObject(j).getString("textColor");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        fontSize = subChildren.getJSONObject(j).getString("fontSize");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        style = subChildren.getJSONObject(j).getString("style");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    int padding=0;
                    try {
                        padding = Integer.parseInt(horizontalPadding);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    LinearLayout.LayoutParams llChild = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    RelativeLayout.LayoutParams paramsll2 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    if(rightAlignmen.equalsIgnoreCase("1")){
                        llChild.gravity = Gravity.RIGHT;
                    }

                    if(type.equalsIgnoreCase("label")){
                        TextView listText1 = new TextView(context);
                        if(field.equalsIgnoreCase("workkardNumber")){
                            listText1.setId(1234);
                            textView[0]=listText1;
                        }
                        if(style.equalsIgnoreCase("upper")){
                            //listText1.setTypeface(null, Typeface.BOLD);
                            listText1.setText(this.getWorkkardMethod(field).toUpperCase());
                        }else{
                            listText1.setText(this.getWorkkardMethod(field));
                        }
                        if(field.equalsIgnoreCase("lossDate")){
                            listText1.setText("Loss Date:"+this.getWorkkardMethod(field));
                            paramsll2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                        }
                        if(field.equalsIgnoreCase("reportedDate")){
                            listText1.setText("Reported:"+this.getWorkkardMethod(field));
                            paramsll2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                        }
                        if (!backgroundColor.equalsIgnoreCase("")) {
                            ShapeDrawable roundRect1=new ShapeDrawable(new RoundRectShape(new float[]{10,10,10,10,10,10,10,10}, null, null));
                            roundRect1.getPaint().setColor(Color.parseColor(backgroundColor));
                            listText1.setBackgroundDrawable(roundRect1);
                        }
                        listText1.setTextColor(Color.parseColor(textColor));
                        if (!fontSize.equalsIgnoreCase("")) {
                            listText1.setTextSize(Integer.parseInt(fontSize));
                        }
                        listText1.setPadding(padding,padding, padding, padding);
                        if(alignment.equalsIgnoreCase("left")){
                            paramsll2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
                        }
                        if(alignment.equalsIgnoreCase("right")){
                            llChild.gravity=Gravity.RIGHT;
                        }
                        if(alignment.equalsIgnoreCase("centre")){
                            paramsll2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
                        }
                        if(bold.equalsIgnoreCase("1")){
                            listText1.setTypeface(null, Typeface.BOLD);
                        }
                            ll.addView(listText1);
                            ll.setLayoutParams(paramsll2);
                    }else if(type.equalsIgnoreCase("spacer")){
                        TextView listText1 = new TextView(context);
                        listText1.setWidth(Integer.parseInt(widthChild));
                        listText1.setText("");
                        ll.addView(listText1);
                    }
                }
                ht.put("i", ll);
                workkardrowLayout.addView((View) ht.get("i"));
            }
        }
    } catch (Exception e) {

        e.printStackTrace();
    }
    return workkardrowLayout;
}
String getWorkkardMethod(String fieldName){
    String work="";
    if(fieldName.equalsIgnoreCase("template")){
        work=workkardData.getTemplate();
    }else if(fieldName.equalsIgnoreCase("workkardTitle")){
        work=workkardData.getWorkkardTitle();
    }else if(fieldName.equalsIgnoreCase("workkardDescription")){
        work=workkardData.getWorkkardDescription();
    }else if(fieldName.equalsIgnoreCase("workType")){
        work=workkardData.getWorkType();
    }else if(fieldName.equalsIgnoreCase("workkardNumber")){
        work=workkardData.getWorkkardNumber();
    }else if(fieldName.equalsIgnoreCase("claimantName")){
        work=workkardData.getClaimantName();
    }else if(fieldName.equalsIgnoreCase("reportedDate")){
        work=workkardData.getReportedDate();
    }else if(fieldName.equalsIgnoreCase("lossDate")){
        work=workkardData.getLossDate();
    }else if(fieldName.equalsIgnoreCase("lossType")){
        work=workkardData.getLossType();
    }else if(fieldName.equalsIgnoreCase("address")){
        work=workkardData.getAddress();
    }else if(fieldName.equalsIgnoreCase("county")){
        work=workkardData.getCounty();
    }else if(fieldName.equalsIgnoreCase("state")){
        work=workkardData.getState();
    }else if(fieldName.equalsIgnoreCase("zip")){
        work=workkardData.getZip();
    }
    return work;
}
}

2 个答案:

答案 0 :(得分:5)

Puneet我很高兴为您提供帮助,每次在Layout中添加视图时,您都会覆盖相同的Layout params,我为您编写了一个代码。

public class MainActivity extends Activity {

RelativeLayouts.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ImageView iv1=new ImageView(this.getApplicationContext());
    iv1.setId(1);
    iv1.setImageResource(R.drawable.ic_launcher);

    ImageView iv2=new ImageView(this.getApplicationContext());
    iv2.setId(2);
    iv2.setImageResource(R.drawable.pdf);

    ImageView iv3=new ImageView(this.getApplicationContext());
    iv3.setId(3);
    iv3.setImageResource(R.drawable.pictureicon);

    ImageView iv4=new ImageView(this.getApplicationContext());
    iv4.setId(4);
    iv4.setImageResource(R.drawable.login_btn);


    RelativeLayout rl =new RelativeLayout(this.getApplicationContext());
    RelativeLayout.LayoutParams paramsrl = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    rl.setLayoutParams(paramsrl);

    RelativeLayout.LayoutParams paramsrl1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    paramsrl1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    iv1.setLayoutParams(paramsrl1);

    RelativeLayout.LayoutParams paramsrl2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    paramsrl2.addRule(RelativeLayout.RIGHT_OF, 1);
    iv2.setLayoutParams(paramsrl2);

    RelativeLayout.LayoutParams paramsrl3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    paramsrl3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    iv3.setLayoutParams(paramsrl3);

    RelativeLayout.LayoutParams paramsrl4 = new     RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    paramsrl4.addRule(RelativeLayout.LEFT_OF, 3);
    iv4.setLayoutParams(paramsrl4);

    rl.addView(iv1, paramsrl1);
    rl.addView(iv2, paramsrl2);
    rl.addView(iv3, paramsrl3);
    rl.addView(iv4, paramsrl4);
    setContentView(rl);
}
}

答案 1 :(得分:0)

使用params.gravityLinearLayout进行对齐。下面给出了示例代码。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
);
params.gravity = Gravity.CENTER_HORIZONTAL; 
textView.setLayoutParams(params);