我无法在编程方式下以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);
答案 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);