我的网格包含七行五ImageView
个。我想将图像置于视图中心。我已经尝试将xml中的每个布局,重力和foregroundGravity属性设置为居中,但似乎没有任何内容。我阅读了一些stackoverflow帖子,他们都建议做我已经尝试过的事情。
网格比右侧更靠近左侧,当我触摸图像时,突出显示的区域在图像右侧比左侧有更多空间。
有关如何让他们居中的任何建议吗?
这是我的布局文件:
<ImageView
android:id="@+id/grid_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:foregroundGravity="center"
>
</ImageView>
</LinearLayout>
这是我在以下课程中使用的课程:
class CustomGrid extends BaseAdapter {
private Context context;
private final List<Button> buttons;
CustomGrid(Context c, List<Button> buttons) {
context = c;
this.buttons = buttons;
}
@Override
public int getCount() { return buttons.size(); }
@Override
public Object getItem(int position) { return null; }
@Override
public long getItemId(int position) { return 0; }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = inflater.inflate(R.layout.grid_single, null);
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
imageView.setImageResource(buttons.get(position).getBasicImageId());
buttons.get(position).setImageView(imageView);
}
else {
grid = convertView;
}
return grid;
}
}
这就是图像。目前我只使用Android图片。
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
</vector>