我有一个带有 4 个 ImageView 的 RelativeLayout,它应该在顶部有 2 个,在布局的每一侧都有一个,在前两个下方有 2 个 ImageView,在屏幕的每一侧都有一个。
我有 2 个问题,应该在下面的两个 ImageView,不要往下走。并且对齐到屏幕右侧的 2 个 ImageView 应该是一个在另一个之上,但似乎大约 25dp。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
RelativeLayout tl = (RelativeLayout)v.findViewById(R.id.l1);
tl.setBackgroundColor(Color.WHITE);
ImageView imageTopL = new ImageView(getActivity());
imageTopL.setImageResource(R.drawable.bell_dl_256);
imageTopL.setPadding(50, 0, 0, 0);
imageTopL.setId(0);
ImageView imageBottomL = new ImageView(getActivity());
imageBottomL.setImageResource(R.drawable.bell_dl_256);
imageBottomL.setPadding(50, 0, 0, 0);
imageBottomL.setId(1);
ImageView imageBottomR = new ImageView(getActivity());
imageBottomR.setImageResource(R.drawable.bell_dr_256);
imageBottomR.setPadding(0, 0, 50, 0);
imageBottomR.setId(2);
ImageView imageTopR = new ImageView(getActivity());
imageTopR.setImageResource(R.drawable.bell_dr_256);
imageTopR.setPadding(0, 0, 50, 0);
imageTopR.setId(3);
tl.addView(imageTopL);
tl.addView(imageTopR);
tl.addView(imageBottomL);
tl.addView(imageBottomR);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageTopL.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params = (RelativeLayout.LayoutParams)imageTopR.getLayoutParams();
params.addRule(RelativeLayout.RIGHT_OF, imageTopL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params = (RelativeLayout.LayoutParams)imageBottomL.getLayoutParams();
params.addRule(RelativeLayout.BELOW, imageTopL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params = (RelativeLayout.LayoutParams)imageBottomR.getLayoutParams();
params.addRule(RelativeLayout.BELOW, imageTopL.getId());
params.addRule(RelativeLayout.RIGHT_OF, imageBottomL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
return v;