面对在RelativeLayout中对齐TextViews的挑战

时间:2018-04-03 13:21:09

标签: android android-layout

我无法在编程方式下以LinearLayout存在的RelativeLayout内对齐TextViews。

 RelativeLayout relativeLayout=new RelativeLayout(getActivity());
       relativeLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));


        TextView operationType = new TextView(getActivity());
        operationType.setId(View.generateViewId());
        operationType.setText(strOperationType);
        operationType.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        relativeLayout.addView(operationType);

        TextView operationDate = new TextView(getActivity());
        operationDate.setText(strOperationDate);
        RelativeLayout.LayoutParams rLLP=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        rLLP.addRule(RelativeLayout.RIGHT_OF,operationType.getId());
        **//rLLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,operationType.getId()); but this pushes the LinearLayout with "C" character displayed in below picture**
        operationDate.setLayoutParams(rLLP);
        relativeLayout.addView(operationDate);

        innerLLayout.addView(relativeLayout);

从下图可以看出,我希望将日期放在手术的右侧和剩余空间的中心,而不仅仅是在手术后。 enter image description here

2 个答案:

答案 0 :(得分:0)

如果有效,请尝试此操作。

operationDate.setGravity(RelativeLayout.CENTER_IN_PARENT);

答案 1 :(得分:0)

尝试更改此行。

operationType.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

然后添加

operationDate.setGravity(RelativeLayout.CENTER_IN_PARENT);