Android 帧布局中控制组件位置

  FrameLayout帧布局,顾名思义。FrameLayout为每个加入其中的组件创建一个空白区域(一帧),默认所有的子元素都放在这块区域的左上角,后面的子元素直接覆盖在前面的子元素之上,将前面的子元素遮挡。帧布局常用场景是充当父布局角色,直接引入填充完整页面,这种情况其实也就是一帧,不需要考虑元素重叠调整子元素位置。当我们在页面某个地方叠加控件时毫无疑问帧布局再合适不过了,我们该如何调整子元素位置呢

我们可以根据layout_gravity属性控制大概方位,结合layout_marginRight layout_marginTop等属性综合调试效果

代码示例如下:

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/one_fragment"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="none"
        android:scrollingCache="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_marginTop="30dp"
        android:layout_marginRight="50dp"
        android:layout_gravity="top|right"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:entries="@array/ctype">
    </Spinner>
</FrameLayout>

效果如下

上面就是帧布局中调整子元素示例,再说一个小插曲  大家可以看到页面整体效果,底部两个按钮是对应两个fragment,其实这个Spinner我只想在按钮1的fragment中使用,刚开始时候我是直接在activity的xml中添加Spinner

 这样显示出来效果两个fragment都有这个下拉框,但是第一个fragment下拉框不可点击,第二个可以正常使用(第二个fragment是空白)于是找原因,第一个fragment下拉框不可点击是因为fragment中有recyleview,滑动冲突了  于是把下拉框放在第一个fragment对应xml中,问题解决。

控件复杂时,大家一定要注意对应层级,要不然会引发事件滑动冲突点击没反应问题。

猜你喜欢

转载自blog.csdn.net/weixin_54723630/article/details/126657566
今日推荐