对齐图像-Android

对齐图像-Android,android,imageview,Android,Imageview,我希望第一个图像距离屏幕左侧20px,右侧图像距离屏幕右侧20px,并且尝试了各种方法,但无法使其工作 这是mycode: View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false); TableLayout tl = (TableLayout)v.findViewById(R.id.l1); TableRow tr = new TableRow(getActivity()); Imag

我希望第一个图像距离屏幕左侧20px,右侧图像距离屏幕右侧20px,并且尝试了各种方法,但无法使其工作

这是mycode:

View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

    TableLayout tl = (TableLayout)v.findViewById(R.id.l1);
    TableRow tr = new TableRow(getActivity());

    ImageView imageL = new ImageView(getActivity());
    imageL.setImageResource(R.drawable.bell_dl_256);
    imageL.setScaleX((float) 0.50);
    imageL.setScaleY((float) 0.5);

    ImageView imageR = new ImageView(getActivity());
    imageR.setImageResource(R.drawable.bell_dr_256);
    imageR.setScaleX((float) 0.5);
    imageR.setScaleY((float) 0.5);

    tr.addView(imageL);
    tr.addView(imageR);
    tl.addView(tr);

    return v;
在您案例中的子元素、图像视图上使用而不是TableLayout和android:layout\u alignParentRight和
android:layout\u alignParentLeft
。要使图像视图远离边缘,只需在图像视图上使用边距
android:layout\u marginLeft
android:layout\u marginRight


此外,建议您使用dp而不是px。px是屏幕像素密度的绝对测量值,dp是屏幕像素密度的相对测量值。第二种方法最适合不同大小的屏幕。

在表格上使用piddingLeft和PaddingRight。也可以尝试使用“边距”属性。谢谢,这很好,我如何分别将图像左对齐和右对齐,以便它们与每个边距对齐?