我有一个带有4个ImageViews的RelativeLayout,它应该有两个在顶部,一个在Layout的每一侧,2个ImageViews在前两个下面,一个在屏幕的每一边。
我有2个问题,两个应该在下面的ImageViews,不要低于。对齐在屏幕右侧的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;
答案 0 :(得分: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;
}