【问题标题】:How do I load two fragments vertically in the same layout?如何在同一布局中垂直加载两个片段?
【发布时间】:2013-11-22 06:15:15
【问题描述】:

我正在为我大学的 SafeWalk 计划制作一个应用程序,并且我正在尝试制作一个活动,该活动将显示一个地图片段,其下方有一个带有紧急呼叫按钮的按钮栏。我遇到的问题是,当我加载以下布局文件时,只显示地图片段,而不是底部的按钮。

注意:我还在此布局中使用了导航抽屉,因此我已包含该代码以防冲突。

我已尝试更改两个父 FrameLayouts 的 layout_width 和 layout_height 值 我只能获得两个结果中的一个,一整页地图,或者一整页我的按钮(拉伸到顶部)。如有必要,我可以附加 CallButtonFragment 的布局,但它是水平线性布局,按钮栏样式包含两个按钮。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/content_frame" >

        <fragment
            android:id="@+id/titles"
            android:name="edu.purdue.SafeWalk.CallButtonFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
</RelativeLayout>
<!-- The navigation drawer -->

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

按照下面 Android-er-naut 的建议,我得到了这个结果: (这是一个改进,但按钮需要更大一些,并适合屏幕的宽度。)

【问题讨论】:

  • 除了采用RelativeLayout 并采用LinearLayout 并将其方向设置为垂直
  • 没有变化,地图占据了整个屏幕。
  • @vidia 即使您将match_parent 高度更改为其他值,这样每个片段都不会像全屏一样高?
  • 但是片段中的布局属性是否适用于 FrameLayout 父级而不是全屏?

标签: android android-layout android-ui android-framelayout


【解决方案1】:

这个怎么样 -

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- The main content view -->

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <fragment
        android:id="@+id/titles"
        android:name="edu.purdue.SafeWalk.CallButtonFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

</LinearLayout>

【讨论】:

  • 好吧,我在 AVD 上试了一下(我的真手机上没有电缆),但由于 AVD 没有播放服务而导致崩溃……哎呀。我回家后会对其进行测试,但我相信我已经尝试过线性布局,但它没有用。不过再试一次也无妨。
  • 我根据您的建议更新了我的答案。按钮现在正在显示,但它们需要更大...
猜你喜欢
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多