0

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

在此处输入图像描述

但是我无法在linearLayout中对齐我的视图,例如名称应该在最左边,其余视图应该右对齐,例如我们通过XML UI在此完成的。

在此处输入图像描述

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

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

我已经试过了

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

这是我的代码,我尝试添加视图来为列表创建一行。

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;
}
}
4

2 回答 2

6

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

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);
}
}
于 2013-05-20T12:12:22.037 回答
0

用于params.gravity对齐LinearLayout. 下面给出的示例代码。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
);
params.gravity = Gravity.CENTER_HORIZONTAL; 
textView.setLayoutParams(params);
于 2020-03-06T17:27:45.763 回答