Android - 适配器内的中心图像

Android - Center image inside adapter

我希望我的 ImageViewTextView 在我的 ViewHolder 中居中。

当我点击它时它看起来像的视觉表示:

它被推到左边,所以当我点击它时,它也 'highlights' 在图像视图的右边。

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(mContext);
        final ViewHolder holder;

        if (convertView == null) { // if it's not recycled, initialize some
            // attributes
            convertView = inflater.inflate(R.layout.item_level_single, null, true);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView
                    .findViewById(R.id.myImageView);

            holder.number = (TextView) convertView
                    .findViewById(R.id.myImageViewText);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.image.setImageResource(mThumbIds[position]);
        if (position == 0){
            holder.number.setText("1");
        }if (position == 1){
            holder.number.setText("2");
        }if (position == 2){
            holder.number.setText("3");
        }if (position == 3){
            holder.number.setText("4");
        }if (position == 4){
            holder.number.setText("5");
        }if (position == 5){
            holder.number.setText("6");
        }if (position == 6){
            holder.number.setText("7");
        }if (position == 7){
            holder.number.setText("8");
        }if (position == 8){
            holder.number.setText("9");
        }



        return convertView;
    }

    // references to our images
    private Integer[] mThumbIds = {

            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round,

    };


    private static class ViewHolder{
        public ImageView image;
        public TextView number;

    }

}

item_single.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:id="@+id/single"
     >

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_oval_big_white"
        />

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myImageView"
        android:layout_alignTop="@+id/myImageView"
        android:layout_alignRight="@+id/myImageView"
        android:layout_alignBottom="@+id/myImageView"
        android:gravity="center"
        android:text="1"
        android:textColor="#000000" />


</RelativeLayout>

编辑 - 添加了 item_single.xml

Child of RelativeLayout 必须有 属性 centerInParent = true.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/single">

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_oval_big_white"
        android:layout_centerInParent="true"/>

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myImageView"
        android:layout_alignTop="@+id/myImageView"
        android:layout_alignRight="@+id/myImageView"
        android:layout_alignBottom="@+id/myImageView"
        android:gravity="center"
        android:text="1"
        android:textColor="#000000"
        android:layout_centerInParent="true" />

</RelativeLayout>

ImageView 部分的下方 属性 添加。

android:layout_centerHorizontal="true"
android:layout_gravity="center"

If true, centers this child horizontally within its parent.

编辑

你可以试试

 <ImageView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="center"
 android:scaleType="center"
 android:src="@drawable/your_image"
 />

尝试将其添加到 ImageView 中:

android:layout_gravity="center_vertical"

在您的 ImageView 和 TextView 上添加这一行

android:layout_centerInParent="true"

使用以下代码更新您的<ImageView>

<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_oval_big_white"
/>

@H.Brooks 看看你是否使用了 ListView 的 onItemClickListener 而不是仅使用 Image 的点击监听器,然后就会发生这种情况。我建议您按如下方式更改适配器:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(mContext);
        final ViewHolder holder;

        if (convertView == null) { // if it's not recycled, initialize some
            // attributes
            convertView = inflater.inflate(R.layout.item_level_single, null, true);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView
                    .findViewById(R.id.myImageView);

            holder.number = (TextView) convertView
                    .findViewById(R.id.myImageViewText);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.image.setOnClickListener(new View.OnClickListener() {
        @Override
          public void onClick(View v) {
             if (position == 0){
               holder.number.setText("1");
            }if (position == 1){
             holder.number.setText("2");
            }if (position == 2){
            holder.number.setText("3");
            }if (position == 3){
             holder.number.setText("4");
            }if (position == 4){
             holder.number.setText("5");
            }if (position == 5){
             holder.number.setText("6");
            }if (position == 6){
             holder.number.setText("7");
            }if (position == 7){
             holder.number.setText("8");
           }if (position == 8){
            holder.number.setText("9");
           }
          }
        });

        holder.image.setImageResource(mThumbIds[position]);

        return convertView;
    }

    // references to our images
    private Integer[] mThumbIds = {

            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round,

    };


    private static class ViewHolder{
        public ImageView image;
        public TextView number;

    }

}