在相对布局中以编程方式水平对齐视图

时间:2014-09-23 08:23:02

标签: android android-layout relativelayout expandablelistview

我想在RelativeLayout中以编程方式对齐TextViews。像这样:

Title of row
Other views -- TextView 1 TextView2 TextView3 etc.. -- other views

所以我有一个foreach,它是以编程方式添加textViews的循环。 这是我的预告:

for(DocumentField d: listOfFields){
                            if(d.isHighlighted()){
                                TextView txt = new TextView(getApplicationContext());
                                txt.setBackgroundDrawable(getResources().getDrawable(R.drawable.shp_round_corners));
                                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
                                params.setMargins(10, 0, 0, 0);
                                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                                txt.setLayoutParams(params);
                                txt.setTag("field");
                                GradientDrawable drawable = (GradientDrawable)txt.getBackground();
                                if(usedcolorMap.get(code) != null)
                                    drawable.setColor(usedcolorMap.get(code));
                                if(d.isNumeric())
                                    txt.setText(d.convertedNumber());
                                else if(d.isText())
                                    txt.setText(d.getTextValue());
                                else if(d.isDate())
                                    txt.setText(d.convertedDate());
                                String text = txt.getText().toString();
                                if(metrics.densityDpi <= DisplayMetrics.DENSITY_MEDIUM){
                                    if(text.length() > 8)
                                        text = text.substring(0, 8).trim() + "...";
                                }else if(text.length() > 15){
                                    text = text.substring(0, 15).trim() + "...";
                                }
                                txt.setTextColor(Color.WHITE);
                                txt.setTextSize(12);
                                txt.setTypeface(null, Typeface.BOLD);
                                txt.setText(text);
                                ll.addView(txt);
                            }
                        }

和xml:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_marginLeft="3dip"
    android:layout_height="20dip"
    android:id="@+id/doc_fields"
    android:tag="ll"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/doc_tag"
    android:orientation="horizontal" >

此代码的结果是textViews重叠。我想仅使用相对布局来获得更好的性能

我尝试过这条规则:

if(idOltText != 0)
                                    params.addRule(RelativeLayout.LEFT_OF, idOltText);

但仍然无法正常工作..

1 个答案:

答案 0 :(得分:0)

我解决了..这是我的循环更新:

int idOltText = 0;
                        for(DocumentField d: listOfFields){
                            if(d.isHighlighted()){
                                TextView txt = new TextView(getApplicationContext());
                                txt.setBackgroundDrawable(getResources().getDrawable(R.drawable.shp_round_corners));
                                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
                                params.setMargins(3, 0, 0, 0);
                                if(idOltText != 0){
                                    System.out.println(idOltText);
                                    params.addRule(RelativeLayout.RIGHT_OF, idOltText);
                                }
                                txt.setLayoutParams(params);
                                txt.setTag("field");
                                GradientDrawable drawable = (GradientDrawable)txt.getBackground();
                                if(usedcolorMap.get(code) != null)
                                    drawable.setColor(usedcolorMap.get(code));
                                if(d.isNumeric())
                                    txt.setText(d.convertedNumber());
                                else if(d.isText())
                                    txt.setText(d.getTextValue());
                                else if(d.isDate())
                                    txt.setText(d.convertedDate());
                                String text = txt.getText().toString();
                                if(metrics.densityDpi <= DisplayMetrics.DENSITY_MEDIUM){
                                    if(text.length() > 8)
                                        text = text.substring(0, 8).trim() + "...";
                                }else if(text.length() > 15){
                                    text = text.substring(0, 15).trim() + "...";
                                }
                                txt.setTextColor(Color.WHITE);
                                txt.setTextSize(12);
                                txt.setTypeface(null, Typeface.BOLD);
                                txt.setText(text);
                                idOltText++;
                                txt.setId(idOltText);
                                ll.addView(txt);

                            }
                        }