Java 如何在Gridview Android上覆盖播放图标?

Java 如何在Gridview Android上覆盖播放图标?,java,android,gridview,Java,Android,Gridview,我想帮助覆盖一个播放图标在每个网格项目的顶部,如果它包含视频。这是我的GridView布局文件。我试着去做,但没能把所有的事情都联系起来 我是新手 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_he

我想帮助覆盖一个播放图标在每个网格项目的顶部,如果它包含视频。这是我的GridView布局文件。我试着去做,但没能把所有的事情都联系起来

我是新手

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
tools:context="com.techx.storysaver.ImageFragment">

<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:listSelector="@drawable/griditem_selector"
    android:numColumns="2"
    android:drawSelectorOnTop="true"
    android:stretchMode="columnWidth"
    android:verticalSpacing="2dp" />
</FrameLayout>

1。为GridView
项创建一个
布局
XML

grid\u item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="4dp"
    card_view:cardUseCompatPadding="false"
    card_view:cardPreventCornerOverlap="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:scaleType="centerCrop"
            android:src="@drawable/sample_1" />

        <ImageView
            android:id="@+id/icon_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/icon_play"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/image"
            android:padding="8dp"
            android:background="#CCFFFFFF"
            android:text="This is simple title"
            android:textColor="#000000" />
    </RelativeLayout>
</android.support.v7.widget.CardView>
输出:


希望这将有助于~

为什么要以编程方式进行,只需在网格项目视图中添加图标您是否尝试过使用xml或不想使用xml?嗨,我不知道如何在xml中的网格视图顶部添加图标。你能帮我一个忙吗?你能给我一个你期望的布局的图片吗?嗨,我在上面提到了包含gridview的框架布局。嗨,谢谢你的帮助。但我使用的是框架布局,里面有gridview。我没有使用CardView,您必须使用CardView查看网格项目。您已经创建了一个适配器,用于在GridView上填充网格项。阅读我的完整答案。希望你能得到这个。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="4dp"
    card_view:cardUseCompatPadding="false"
    card_view:cardPreventCornerOverlap="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:scaleType="centerCrop"
            android:src="@drawable/sample_1" />

        <ImageView
            android:id="@+id/icon_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/icon_play"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/image"
            android:padding="8dp"
            android:background="#CCFFFFFF"
            android:text="This is simple title"
            android:textColor="#000000" />
    </RelativeLayout>
</android.support.v7.widget.CardView>
@Override
public View getView(int pos, View convertView, ViewGroup parent) 
{
    Arrays.sort(file, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
    final String path = file[position].getAbsolutePath();

    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.grid_item, null);
    }

    ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
    ImageView iconPlay= (ImageView) convertView.findViewById(R.id.icon_play);
    TextView textTitle= (TextView) convertView.findViewById(R.id.title);

    // Image
    if (path.endsWith(".jpg") || path.endsWith(".jpeg") || path.endsWith(".png")) {
        Glide
            .with(context)
            .load(file[position])
            .into(imageView);

        // Hide play icon
        iconPlay.setVisibility(View.GONE);
   }

   // Video
   if (path.endsWith(".mp4")) {
        Glide
            .with(context)
            .load(file[position])
            .into(imageView);

        // Show play icon
        iconPlay.setVisibility(View.VISIBLE);
    }

    return convertView;
}