Seekbar位于可点击的ImageView

时间:2017-04-26 07:48:22

标签: android android-layout gridview

我对此很陌生,如果这很简单就道歉。我有一个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" />

2 个答案:

答案 0 :(得分:0)

Use FrameLayout for this.

<FrameLayout>

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

<Button >
</Button>

</FrameLayout>

答案 1 :(得分:0)

您也可以按照以下方式进行操作

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"

    />

和你的ImageAdapter.java

以下是代码。请注意,这是一种肮脏的编码方式    适配器而不是一个应该膨胀视图。

    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,

        };
    }