以下是我的布局。
我想要的是2个绿色箭头在左上方的正方形中垂直居中。此外,双数字符号(“##”)需要在左上角正方形中垂直和水平居中。
然后,另一组双号标志需要在右上方的正方形中垂直和水平居中。
以下是代码。
public class Puzzle extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dividers();
topLeft();
topRight();
setContentView(relativelayout);
}
public void topLeft() {
RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params0.addRule(RelativeLayout.ABOVE, view0.getId());
params0.addRule(RelativeLayout.LEFT_OF, view1.getId());
relativelayout.addView(topLeftRL, params0);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
topLeftRL.addView(puzzle, params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.CENTER_VERTICAL);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
topLeftRL.addView(left, params2);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.CENTER_VERTICAL);
params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
topLeftRL.addView(right, params3);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.CENTER_IN_PARENT);
topLeftRL.addView(levelCounter, params4);
left.setImageResource(R.drawable.left_arrow);
right.setImageResource(R.drawable.right_arrow);
puzzle.setText(c.getResources().getString(R.string.puzzlePuzzleTV));
levelCounter.setText("###");
}
public void topRight() {
RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params0.addRule(RelativeLayout.ABOVE, view0.getId());
params0.addRule(RelativeLayout.RIGHT_OF, view1.getId());
relativelayout.addView(topRightRL, params0);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
topRightRL.addView(record, params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
topRightRL.addView(moves, params2);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.CENTER_IN_PARENT);
topRightRL.addView(movesCounter, params3);
movesCounter.setText("##");
record.setText(c.getResources().getString(R.string.puzzleRecordTV) + " " + "##");
moves.setText(c.getResources().getString(R.string.puzzleMovesTV));
}
public void dividers() {
// vertical line (top of screen)
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(5, height / 6);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
params1.setMargins(0, 10, 0, 10); // left, top, right, bottom
relativelayout.addView(view1, params1);
// horizontal line (top of screen)
RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 5);
params0.addRule(RelativeLayout.BELOW, view1.getId());
params0.setMargins(10, 0, 10, 0); // left, top, right, bottom
relativelayout.addView(view0, params0);
// horizontal line (button of screen)
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 5);
params3.addRule(RelativeLayout.ABOVE, linearlayout.getId());
relativelayout.addView(view3, params3);
}
}
所以看起来当我向param添加多个addRule()
时,它不会同时读取它们。我认为问题在于topLeft()
方法的这一行:params0.addRule(RelativeLayout.ABOVE, view0.getId());
。我已尝试在该参数中使用其他addRule切换此行,结果相同。这是为什么?
编辑#1 - 清理我的代码,使整洁,更容易阅读。
答案 0 :(得分:0)
您正在使用需要锚点的参数。你需要使用“addRule(int verb,int anchor)”
所以喜欢
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP,1);
答案 1 :(得分:0)
我找到了自己问题的答案。错误是我的onCreate()
方法的一部分,我没有包含在我的开场白中!这是问题代码:
view0.setId(0);
显然,id不能设置为0.无论如何,我将身份证号码更改为正数,一切正常。