繁体   English   中英

如何在GridView中的ImageView上设置ImageView

[英]How to set ImageView over ImageView in GridView

假设我要在另一个图像上设置多个图像,如何在GridView中做到这一点?

喜欢:
背景
ImageView1
ImageView2右下角的ImageView2

这是我想要的示例: http : //i.stack.imgur.com/JS36H.png

这是我的密码
活动:

public class ImgAdapter extends BaseAdapter {
    private Context mContext;
    private ColorMatrixColorFilter cf;
    String Questions = Question.Questions;
    int level = Levels.level;

    public ImgAdapter(Context c) {
        mContext = c;
        ColorMatrix matrix = new ColorMatrix();
        //matrix.setSaturation(0); //0 means grayscale
        matrix.setSaturation(0);
        cf = new ColorMatrixColorFilter(matrix);
    }

    public int getCount() {

        return ThumbIds.length;

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        SharedPreferences pref = mContext.getSharedPreferences(Questions, Context.MODE_PRIVATE);


        //imageView = (ImageView) convertView;
        imageView = new ImageView(mContext);
        int size = mContext.getResources().getDimensionPixelSize(R.dimen.width);
        imageView.setLayoutParams(new GridView.LayoutParams(size, size));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setAdjustViewBounds(true);
        imageView.setImageResource(ThumbIds[position]);



        if(pref.getBoolean(level+"answered"+position, false)){

            //Thats the ImageView I want to set over the another ImageView
            /*ImageView answ;
            answ = new ImageView(mContext);
            answ.setScaleType(ImageView.ScaleType.CENTER_CROP);
            answ.setAdjustViewBounds(true);
            answ.setImageResource(R.drawable.answered);*/

            imageView.setImageResource(ThumbIds[position]+1);
        }

        return imageView;

    }


    public static Integer[] ThumbIds =
        {
        R.drawable.1,
        R.drawable.2,
        R.drawable.3,
        R.drawable.4,
        R.drawable.5,
        R.drawable.6,
        R.drawable.7
        };  

}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:id="@+id/layout" >

    <TextView
        android:id="@+id/Correct"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        />

<GridView 
android:id="@+id/Question"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"
android:numColumns="3"
android:stretchMode="columnWidth"
tools:context=".Question" />

</LinearLayout>

在Question活动中,我得到GridView

不幸的是,当单元试图相互重叠时,GridView不喜欢它。 假设您在gridview中有一个单元格为48x48dp。 尝试渲染大于48的任何内容都会导致其被裁剪。 因此,这意味着您需要做一些技巧。

假设您要以48x48dp显示主图像。 让我们称第二个人为徽记,其徽记为16x16dp,并且您希望它居中位于主图像的右下角。 然后,您将需要一个大约56dp的单元。 如果将填充或边距添加到混合中,则可能需要进行调整。 对GridView中每个项目的布局执行类似于以下操作的技巧。

<FrameLayout
    android:layout_width="56dp"
    android:layout_height="56dp"
    ....any other attributes>

    <ImageView
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="left|top"
        android:src="Your main image"
        ...any other attributes/>

    <ImageView
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_gravity="right|bottom"
        android:src="Your main image"
        ...any other attributes/>

</FrameLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM