0

我有一个带有 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;
4

1 回答 1

1
// try this
  @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(this);
        imageTopL.setId(1);
        imageTopL.setImageResource(R.drawable.ic_launcher);
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageTopL.setAdjustViewBounds(true);
        params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params1.setMargins(50,10,0,0);
        imageTopL.setLayoutParams(params1);
        imageTopL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ImageTextListViewActivity.this,"Top Left",Toast.LENGTH_SHORT).show();
            }
        });


        ImageView imageTopR = new ImageView(this);
        imageTopR.setId(2);
        imageTopR.setImageResource(R.drawable.ic_launcher);
        imageTopL.setAdjustViewBounds(true);
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params2.setMargins(0,10,50,0);
        imageTopR.setLayoutParams(params2);
        imageTopR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ImageTextListViewActivity.this,"Top Right",Toast.LENGTH_SHORT).show();
            }
        });


        ImageView imageBottomL = new ImageView(this);
        imageBottomL.setId(3);
        imageBottomL.setImageResource(R.drawable.ic_launcher);
        imageTopL.setAdjustViewBounds(true);
        RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params3.addRule(RelativeLayout.BELOW, imageTopL.getId());
        params3.setMargins(50,10,0,0);
        imageBottomL.setLayoutParams(params3);
        imageBottomL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ImageTextListViewActivity.this,"Bottom Left",Toast.LENGTH_SHORT).show();
            }
        });

        ImageView imageBottomR = new ImageView(this);
        imageBottomR.setId(4);
        imageBottomR.setImageResource(R.drawable.ic_launcher);
        imageTopL.setAdjustViewBounds(true);
        RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params4.addRule(RelativeLayout.BELOW, imageTopR.getId());
        params4.setMargins(0,10,50,0);
        imageBottomR.setLayoutParams(params4);
        imageBottomR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ImageTextListViewActivity.this,"Bottom Right",Toast.LENGTH_SHORT).show();
            }
        });

        tl.addView(imageTopL);
        tl.addView(imageTopR);
        tl.addView(imageBottomL);
        tl.addView(imageBottomR);

        return v;
    }
于 2013-10-24T09:21:15.460 回答