Android 可单击图像视图顶部的搜索栏

Android 可单击图像视图顶部的搜索栏,android,android-layout,gridview,Android,Android Layout,Gridview,我对这件事还不熟悉,所以如果简单的话,我道歉。我有一个2乘4的GridLayout,图像可以点击,图像可以改变,声音可以播放 我想在每个图像的顶部添加一个音量控制,它可以控制图像的音量。我该怎么做 <GridLayout android:id="@+id/mainGrid" android:layout_width="0dp" android:layout_height="0dp" android:background="@c

我对这件事还不熟悉,所以如果简单的话,我道歉。我有一个2乘4的GridLayout,图像可以点击,图像可以改变,声音可以播放

我想在每个图像的顶部添加一个音量控制,它可以控制图像的音量。我该怎么做

<GridLayout
        android:id="@+id/mainGrid"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/whitenoise"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:adjustViewBounds="true"
            android:background="@android:color/transparent"
            android:onClick="whiteNoiseTapped"
            android:scaleType="fitXY"
            android:text="White 1"
            app:srcCompat="@drawable/whitenoise" />

为此使用框架布局。
android:id=“@+id/whitenoise”
android:layout\u width=“包装内容”
android:layout\u height=“包装内容”
android:layout\u column=“0”
android:layout\u columnWeight=“1”
android:layout_gravity=“fill”
android:layout\u row=“0”
android:layout_rowWeight=“1”
android:adjustViewBounds=“true”
android:background=“@android:color/transparent”
android:onClick=“whiteNoiseTapped”
android:scaleType=“fitXY”
android:text=“白色1”
app:srcCompat=“@drawable/whitenoise”

您也可以按以下方式执行

layout.xml to some extent like this...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#000"
tools:context="com.example.tistis.myapplication.MainActivity">

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:numColumns="2"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"

    />

谢谢然后我会把所有的ImageView放在那些FrameLayout标签中吗?我只是想试试这个,代码会给我各种各样的错误…?'布局高度应定义为“在框架布局上设置高度和宽度,并将按钮放在按钮标签内”
layout.xml to some extent like this...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#000"
tools:context="com.example.tistis.myapplication.MainActivity">

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:numColumns="2"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"

    />
    class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public int screenHeight,screenWidth;
        public ImageAdapter(Context c) {
            mContext = c;
            DisplayMetrics displaymetrics = new DisplayMetrics();
            ((Activity)c).getWindowManager().
               getDefaultDisplay().getMetrics(displaymetrics);
            screenHeight = displaymetrics.heightPixels;
            screenWidth = displaymetrics.widthPixels;
        }

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

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

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


        public View getView(final int position, 
         View convertView, ViewGroup parent) {
            ImageView imageView=null;
            RelativeLayout frm;
            SeekBar seekBar=null;
            if (convertView == null) {
                // if it's not recycled, initialize some attributes
                frm= new RelativeLayout(mContext);
                frm.setLayoutParams(new GridView.LayoutParams(screenWidth/2-(8*2+10),screenWidth/2-(8*2+10)));
                frm.setPadding(8, 8, 8, 8);
                frm.setBackgroundColor(Color.BLACK);

                imageView = new ImageView(mContext);
                imageView.setLayoutParams(
                 new FrameLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.MATCH_PARENT));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setBackgroundColor(Color.BLACK);
                frm.addView(imageView);

                imageView.setOnClickListener(
                   new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(mContext,
                        "Item position "+(position+1),
                        Toast.LENGTH_SHORT).show();
                    }
                });


                seekBar=new SeekBar(mContext);

                seekBar.setLayoutParams(
                 new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.MATCH_PARENT, 40));
                RelativeLayout.LayoutParams lp=
             (RelativeLayout.LayoutParams) 
               seekBar.getLayoutParams();
                lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                seekBar.setMax(15);
    //      seekBar.setIndeterminate(true);

                ShapeDrawable thumb = 
                 new ShapeDrawable(new OvalShape());

                thumb.setIntrinsicHeight(80);
                thumb.setIntrinsicWidth(30);
                seekBar.setThumb(thumb);
                seekBar.setProgress(1);
                seekBar.setVisibility(View.VISIBLE);
                seekBar.setBackgroundColor(Color.TRANSPARENT);

                frm.addView(seekBar);

            } else {
                frm = (RelativeLayout) convertView;
            }


            if(imageView!=null)
                imageView.setImageResource(mThumbIds[position]);
            return frm;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.superman, R.drawable.superman,
                R.drawable.superman, R.drawable.superman,

        };
    }