【发布时间】:2017-11-24 21:23:09
【问题描述】:
在我第一次尝试使用 Android(Studio 3.0)时,我设法实现了 bottom navigation 和 fragments。
现在在第一个 Fragment 上实现小部件时,我意识到底部导航正在覆盖/隐藏 Fragment 的最下部。
Fragment 由两个布局组成,一个列表视图和两个按钮:
<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"
tools:context="com.example.xyz.ticketvendor.QuickRideFragment">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/lvTransportLines"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignBottom="@id/lvTransportLines">
<Button
android:id="@+id/getTicket"
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight=".75"
android:text="Get Ticket" >
</Button>
<Button
android:id="@+id/setDefaultSMSApp"
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight=".25"
android:text="Reset" >
</Button>
</LinearLayout>
</RelativeLayout>
如您所见,我正在使用 android:layout_alignBottom="@id/lvTransportLines" 将 LinearLayout(保留 2 个按钮)定位在 Listview 下方。
我只能看到按钮,因为我将它们的高度设置为用于测试目的的东西很大。
但是 - 我可以使用哪个布局属性将 LinearLayout 定位得足够高,以免被底部导航隐藏?谢谢!
更新 在实施@Nero 的解决方案后,我想出了这个:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.antibala.ticketvendor.QuickRideFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/lvTransportLines"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/getTicket"
android:layout_width="0dp"
android:layout_height="140dp"
android:layout_weight=".75"
android:text="Get Ticket"
>
</Button>
<Button
android:id="@+id/setDefaultSMSApp"
android:layout_width="0dp"
android:layout_height="140dp"
android:layout_weight=".25"
android:text="Reset"
>
</Button>
</LinearLayout>
</RelativeLayout>
但我得到的结果与我的第一张照片完全相同。我做错了什么?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xyz.ticketvendor.MainActivity">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
将您的父布局设为相对布局,将您的列表视图放在其中,然后为您的按钮创建线性布局(水平方向并设置对齐父底部为真)。我认为它的框架布局导致重叠
-
@Nero,我相信您已经正确实施了您的解决方案......并更新了我的帖子。不知何故,它并没有解决问题:|
-
请尝试让您的列表视图高度包装内容并分享您的活动的xml代码
-
我做到了。你能再看看吗? ;)
-
在您的主要活动 XML 文件中,将您的框架布局更改为线性布局(尽管它不应该产生影响),如果这不起作用,请给我一些时间回家解决这个问题
标签: java android android-layout android-fragments android-studio-3.0