android - 如何为 LinearLayout 中的 Fragments 分配比例尺寸?

标签 android android-fragments

我试图在单个 Activity 中垂直放置 3 个不同的 Fragment

我愿意:

  • 为每个 Fragment 分配一个比例(最好是百分比)高度。
  • fragment 应占据整个设备宽度。
  • 根据旋转改变它的位移。

这是我的 Activity 布局的样子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/messageFragment"
        android:name="apps.android.expressa.expressa.Fragments.MessageFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/message_fragment_layout" />

    <fragment
        android:id="@+id/listsFragment"
        android:name="apps.android.expressa.expressa.Fragments.ListsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        tools:layout="@layout/lists_fragment_layout" />

    <fragment
        android:id="@+id/bottomBarFragment"
        android:name="apps.android.expressa.expressa.Fragments.BottomBarFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        tools:layout="@layout/bottom_bar_layout" />


</LinearLayout>

问题是现在,只有第一个和第二个 fragment 显示,第三个只能看到顶部。请注意,我现在是 Android 的新手。

最佳答案

希望对你有帮助

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:weightSum="6" >

<fragment
    android:id="@+id/messageFragment"
    android:name="apps.android.expressa.expressa.Fragments.MessageFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    tools:layout="@layout/message_fragment_layout" />

<fragment
    android:id="@+id/listsFragment"
    android:name="apps.android.expressa.expressa.Fragments.ListsFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    tools:layout="@layout/lists_fragment_layout" />

<fragment
    android:id="@+id/bottomBarFragment"
    android:name="apps.android.expressa.expressa.Fragments.BottomBarFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    tools:layout="@layout/bottom_bar_layout" />

</LinearLayout>

Screenshot

关于android - 如何为 LinearLayout 中的 Fragments 分配比例尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29530845/

相关文章:

android - 如何用 Fragment 替换 ViewPager

android - 更改详细信息 fragment 上列表 fragment 的字体

android - 无法从 CursorWindow 读取第 1 行,col -1。在从游标访问数据之前确保正确初始化了游标

android - cd android && ./gradlew assembleRelease '.' 不是内部或外部命令,也不是可运行的程序或批处理文件

android - 如何仅在打开应用程序时接收 Parse 推送通知,并以模态对话框形式显示

android - 如果有 getArguments(),我们是否应该在 fragment 中使用 savedInstanceState?

android - 导航架构组件-如何刷新 fragment ?

android - 设计一个仅限安卓平板电脑的应用程序

android - 请参见测试启动状态错误

android - 包含 fragment 的框架布局中的 View 的 findViewById 失败