【问题标题】:Problem positionning buttons under a google map fragment in a bottom navigation layout底部导航布局中谷歌地图片段下的问题定位按钮
【发布时间】:2019-02-24 11:46:08
【问题描述】:

我有一个带有三个标签的底部导航应用。在应用程序的一个屏幕上,我有一个谷歌地图片段。我希望仅在此屏幕的地图片段下方和底部导航上方显示三个按钮。

当我在这个特定片段的设计器中时,它显示了我想要的(这里灰色的“片段”实际上是谷歌地图片段)

你可以从github下载代码:https://github.com/guyprovost/BottomBug/tree/master

但是当我运行应用程序时,我得到了:

我的目标是这个最终结果,(很抱歉对预期结果的解释不佳,但你明白了!)

这是片段布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    class="com.google.android.gms.maps.MapFragment"
    android:layout_marginTop="24dp" />
<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/controlBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/primaryDark"
    android:gravity="bottom">
    <Button
        android:id="@+id/testbutton1"
        android:text="One"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_weight=".30"/>
    <Button
        android:id="@+id/testbutton2"
        android:text="Two" 
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_weight=".40" />
    <Button
        android:id="@+id/testbutton3"
        android:text="Three" 
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_weight=".30" />
</LinearLayout>

这是主要的活动布局文件:

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation" />
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="start"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        app:elevation="16dp"
        app:menu="@menu/bottom_navigation_main" />
</RelativeLayout>

这是底部导航菜单的xml文件内容:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_map"
        android:enabled="true"
        android:icon="@drawable/ic_map"
        android:title="@string/tab1_title"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/menu_history"
        android:enabled="true"
        android:icon="@drawable/ic_history"
        android:title="@string/tab2_title"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/menu_settings"
        android:enabled="true"
        android:icon="@drawable/ic_settings"
        android:title="@string/tab3_title"
        app:showAsAction="ifRoom" />
</menu>

我看起来要么无法在运行时正确操作地图片段高度,要么不能像在底部导航布局中那样使用它......或者我在某个地方搞砸了,这很有可能。

有什么线索吗?

【问题讨论】:

  • 尝试通过代码而不是xml管理MapFragment
  • 我不确定我是否理解您的提示,但渴望学习...
  • 可能的错误是您在片段的布局中使用了权重,但没有相应地为LinearLayout 分配权重。这样做,请使用match_parent 而不是fill_parent
  • 在主要活动布局中尝试为框架布局提供 56dp 底部边距。
  • 试着把这条线放在你的bottomNavigationXml中:android:background="@android:color/transparent"它会让你的底部导航透明,你可以看到你的三个按钮是否真的在底部导航后面。

标签: android android-layout android-fragments xamarin.android


【解决方案1】:

如果我正确理解了您的查询,那么让我给您一个解决方案,您可以使用相对布局而不是像下面这样的线性布局,还让我告诉您,我理解您在主要活动中也需要导航视图您需要导航视图顶部的三个按钮,最重要的是您需要地图片段。对?所以这里是解决方案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_above="@+id/controlBar"
        class="com.google.android.gms.maps.MapFragment"
        android:layout_marginTop="24dp" />
    <LinearLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/controlBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:background="@color/primaryDark"
        android:gravity="bottom">
        <Button
            android:id="@+id/testbutton1"
            android:text="One"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_weight=".30"/>
        <Button
            android:id="@+id/testbutton2"
            android:text="Two" 
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_weight=".40" />
        <Button
            android:id="@+id/testbutton3"
            android:text="Three" 
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_weight=".30" />
    </LinearLayout>

【讨论】:

  • 你好!非常感谢您的回答,但最终结果完全相同。我没有看到按钮,唯一的区别在于设计器渲染。但最终结果完全一样。我不知道我是否理解正确,但我只需要地图片段一中的三个按钮。这是在片段布局中定义的。
【解决方案2】:

试试这个,我在手机上检查过,它工作正常

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

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="com.google.android.gms.maps.MapFragment"
    android:layout_marginTop="24dp" />

<LinearLayout
    android:id="@+id/controlBar"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:background="@color/colorAccent"
    android:gravity="bottom">
    <Button
        android:id="@+id/testbutton1"
        android:text="One"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_weight=".30"/>
    <Button
        android:id="@+id/testbutton2"
        android:text="Two"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_weight=".40" />
    <Button
        android:id="@+id/testbutton3"
        android:text="Three"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_weight=".30" />
</LinearLayout>

</LinearLayout>

activity_main

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation" />
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="start"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        app:elevation="16dp"
        app:menu="@menu/navigation" />
</RelativeLayout>

【讨论】:

  • 感谢您的输入,但是在复制/粘贴您的 axml 之后,我仍然有完全相同的输出。你使用了和我一样的活动布局吗?就像说的那样,问题似乎来自应用程序使用底部导航范例这一事实。可以肯定,活动布局中的相同代码可以正常工作,但与 FrameLayout 集成时就不行了……也许这就是问题所在?
  • 让我再检查一次
  • 好的,我一定会的。我在模拟器上做了测试。在我看来,我在片段布局文件上所做的每一个布局技巧都没有渲染,只是这个文件之外的东西!今天下午我会测试,谢谢你的时间!
  • 刚刚用真机进行了测试,仍然有完全相同的输出!您确定使用活动布局(主要)吗?我正在使用片段管理器在主要活动中膨胀地图片段。我注意到我尝试在片段本身中明智地进行布局的任何事情都被忽略了,它所做的只是拉伸以填充父级!感谢您在这个问题上投入的时间!
猜你喜欢
  • 2019-03-04
  • 2019-11-02
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多