【问题标题】:My Relativelayout won't show under FrameLayout我的 Relativelayout 不会在 FrameLayout 下显示
【发布时间】:2017-09-14 04:24:50
【问题描述】:

enter image description hereOK 所以,我制作了 fragment_googlemap.xml 并将 FrameLayout 设计为根布局,因为我需要将 mapView 作为片段。切入正题,我想做的是在谷歌地图的顶部显示 Spinner 和 Floating Action Button。但是当我运行应用程序时,我看不到 relativelayout 的子项(微调器和浮动按钮)。我认为它在 googlemapview 下,因为加载时我可以在 googlemap 下看到它们一秒钟。 有什么解决办法吗?

OOOHH 和顺便说一句,我不知道为什么浮动按钮中的海拔属性值不起作用任何想法??

对不起,如果我的英语很差:(提前谢谢!!!

=======下面是我的xml文件源代码===========

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/tools"
    android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <Spinner
        android:id="@+id/spinner_countries"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/fab"
        android:layout_below="@+id/fab"
        android:entries="@array/location_arraysResc" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_gravity="end|top"
        android:layout_margin="10dp"
        android:backgroundTint="@color/colorPrimary"
        android:elevation="6dp"
        android:src="@drawable/paperplane"
        android:tint="#000000"
        app:borderWidth="0dp"
        app:rippleColor="#eF43ef" />
    </RelativeLayout>


    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </com.google.android.gms.maps.MapView>


</FrameLayout> 

【问题讨论】:

  • 框架布局必须只有 1 个子级。在放入 framelayout 之前,将您的相对布局和地图放入另一个布局中。
  • 在框架布局中子视图绘制在堆栈中,最近添加的子视图在顶部。因此,在您的情况下,最近添加的子项是 mapview,这就是为什么您的微调器和浮动按钮位于地图后面的原因。把顺序反过来就好了。
  • 最好的布局是什么?把它们放在一起?
  • @Lcukerd 不一定要有一个孩子,但为了正确组织孩子,它应该有一个视图。
  • 是的,颠倒了订单WOORKED !!!!!!!!!谢了!

标签: android android-layout android-relativelayout android-framelayout


【解决方案1】:

试试这个

1. FloatingActionButton 使用 app:elevation="6dp" insted of android:elevation="6dp"

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </com.google.android.gms.maps.MapView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:backgroundTint="@color/colorPrimary"
        android:elevation="6dp"
        android:src="@drawable/ic_info_black_24dp"
        android:tint="#000000"
        app:borderWidth="0dp"
        app:rippleColor="#eF43ef" />

    <Spinner
        android:id="@+id/spinner_countries"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignRight="@+id/fab"
        android:layout_below="@+id/fab"
        android:entries="@array/myarray" />

</RelativeLayout>

【讨论】:

  • 如果 relative 是匹配父级,则地图将不会显示,因为线性布局的方向是垂直的,并且屏幕空间已经被相对布局覆盖。
  • 谢谢!它有效,但我看到一个黄色突出显示一直告诉我:“如果@id/fab 由于本地化文本扩展较少而增长,@id/fab 可以与@id/map 重叠...... (Ctrl+F1) 如果相对布局有文本或按钮项对齐到左侧和右侧,由于本地化文本扩展,它们可以相互重叠,除非它们有相互约束,如 toEndOf/toStartOf" 任何建议?抱歉我的无知,因为我是 android 新手 : (
【解决方案2】:

您可以将FrameLayout 视为LinearLayout,除了它将视图堆叠在一起。布局中的最后一个视图将是屏幕上的顶视图。

因此,如果您想在MapView 上添加一些内容,您应该将这些视图标签放在 &lt;MapView&gt; 标签之后。

【讨论】:

    【解决方案3】:

    您可以将MapView 放入一个新的RelativeLayout 中,试试这个。

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:fab="http://schemas.android.com/tools"
        android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    
        <Spinner
            android:id="@+id/spinner_countries"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/fab"
            android:layout_below="@+id/fab"
            android:entries="@array/location_arraysResc" />
    
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_gravity="end|top"
            android:layout_margin="10dp"
            android:backgroundTint="@color/colorPrimary"
            android:elevation="6dp"
            android:src="@drawable/paperplane"
            android:tint="#000000"
            app:borderWidth="0dp"
            app:rippleColor="#eF43ef" />
        </RelativeLayout>
    
         <RelativeLayout
            android:id="@+id/spinner_countries"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
            <com.google.android.gms.maps.MapView
              android:id="@+id/map"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
            </com.google.android.gms.maps.MapView>
    
       </RelativeLayout
    
    </FrameLayout> 
    

    【讨论】:

    • 您实际上应该首先添加地图视图,然后将浮动按钮添加为框架布局的子视图,因为子视图是在堆栈中绘制的,最近添加的子视图位于顶部
    【解决方案4】:

    试试这个

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:fab="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    </com.google.android.gms.maps.MapView>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Spinner
            android:id="@+id/spinner_countries"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/fab"
            android:layout_below="@+id/fab"
            android:entries="@array/location_arraysResc" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_gravity="end|top"
            android:layout_margin="10dp"
            android:backgroundTint="@color/colorPrimary"
            android:elevation="6dp"
            android:src="@drawable/paperplane"
            android:tint="#000000"
            app:borderWidth="0dp"
            app:rippleColor="#eF43ef" />
    </RelativeLayout>
    

    【讨论】:

    • 谢谢您的意见!
    • @poream3387 工作正常吗?或任何问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 2014-05-17
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    相关资源
    最近更新 更多