Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-Center图像内置适配器_Android_Imageview_Baseadapter - Fatal编程技术网

Android-Center图像内置适配器

Android-Center图像内置适配器,android,imageview,baseadapter,Android,Imageview,Baseadapter,我希望我的图像视图和文本视图集中在我的视图夹中 当我点击它时,它看起来像什么的视觉表现: 它被推到左侧,因此当我单击它时,它也会“高亮显示”imageview的右侧 ImageAdapter.java public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; }

我希望我的图像视图文本视图集中在我的视图夹

当我点击它时,它看起来像什么的视觉表现:

它被推到左侧,因此当我单击它时,它也会“高亮显示”imageview的右侧

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>


编辑-添加的项\u single.xml

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>

相对性对象的子对象必须具有属性
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>

图像视图部分中添加以下属性

android:layout_centerHorizontal="true"
android:layout_gravity="center"
如果为true,则此子对象在其父对象内水平居中

编辑

你可以试试看

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

图像视图部分中添加以下属性

android:layout_centerHorizontal="true"
android:layout_gravity="center"
如果为true,则此子对象在其父对象内水平居中

编辑

你可以试试看

 <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中:

android:layout_gravity="center_vertical"

在ImageView和TextView上添加此行

android:layout_centerInParent="true"

在ImageView和TextView上添加此行

android:layout_centerInParent="true"
使用以下代码更新您的

<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"
/>

使用以下代码更新您的

<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中的McClickListener,并且没有仅使用图像的click listener,则会发生这种情况。我建议您按如下方式更改适配器:

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;

    }

}

@H.Brooks查看您是否使用了ListView中的ClickListener,并且没有仅使用图像的click listener,则会发生这种情况。我建议您按如下方式更改适配器:

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;

    }

}

能否显示适配器行的布局xml结构?将imageview和textview置于相对位置并设置
layout\u centerInParent=“true”
请参见编辑能否显示适配器行的布局xml结构?将imageview和textview置于相对位置并设置
layout\u centerInParent=“true”
请参见编辑