GridView 条目选中放大被遮挡问题

      之前开发了一个在Android TV 上的应用,遇到了一个问题:在鼠标移到菜单选项上的时候,放大该菜单选项,但没想到放大这些菜单选项之后,要么遮挡了其它的菜单选项,要么被其它的菜单选项遮挡了。在网上搜了很多,但是没有解决问题,下面提供我最后想到的解决方案。

   下面为解决前和解决后的对比图:

  

    条目没有选中时,背景颜色为白色,选中时,背景颜色为绿色。

    首先,子View的放大后的宽高一定小于父View的宽高,因此不管GridView条目放大多少倍,它的宽高一定小于GridView的宽高。从第一张图,可以看出每一个条目放大后,都是往外占领区域,它的界限是GridView的宽度和高度,因此,就出现了条目之间遮挡的问题。

    从上面出现的问题,仔细想,如果放大的View的宽高局限于条目的宽高,那么放大之后就不会超出条目,不会往外占领区域,不会遮挡。因此,可以在条目的根View外再嵌套一个父View,然后让第二层View放大,就局限在了条目的宽高之中,如第二张图所示。

    下面我在测试中,第一张图条目的布局和第二张图的布局:

    第一张图GridView条目的布局:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="15dp"
    android:layout_width="match_parent"
    android:id="@+id/layout"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</RelativeLayout></span>
第二张图GridView条目的布局:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:padding="15dp"
        android:layout_width="match_parent"
        android:id="@+id/layout"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_centerInParent="true"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_gravity="center"
                android:src="@drawable/ic_launcher"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:layout_gravity="center"
                android:gravity="center"
                android:text="Hello"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>

    </RelativeLayout>
</FrameLayout>
</span>

注意:放大的View是第二层View,也就是上面id="@+id/layout"的View,另外根View的padding也很重要,因为放大的View的宽高局限在根View的宽高,所以可以利用根View的padding来配合放大的View的倍数。

    附加:这适合与任何情况,只要放大后的View遮挡了相邻的View,就可以在它的外面嵌套一个父View,再配合padding来调制View放大的倍数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值