简体   繁体   English

将ButtonBar添加到布局后,片段消失

[英]Fragments disappears after adding ButtonBar to the layout

I am trying to create a master detail UI with three fragments and two buttons button bar. 我正在尝试创建一个具有三个片段和两个按钮按钮栏的主详细信息UI。 For some reason, the fragments are not showing up in the UI after adding the button bar. 由于某些原因,添加按钮栏后,片段未在UI中显示。 I suspect it has something to do with the orientation of the layout but I'll leave it up to the experts here. 我怀疑这与布局的方向有关,但是我会留给这里的专家。

Before adding the button bar 在添加按钮栏之前

After adding the button bar 添加按钮栏后

Here is the XML layout of the UI 这是UI的XML布局

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

<fragment
    android:id="@+id/mainBodyFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="com.dreamcode.healttracker.MainBodyArea" />

<fragment
    android:id="@+id/subBodyFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="com.dreamcode.healttracker.SubBodyArea" />

<fragment
    android:id="@+id/symptomFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3"
    class="com.dreamcode.healttracker.Symptom" />

<LinearLayout
    android:id="@+id/footer"
    style="@android:style/ButtonBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#F0F0F0"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_Generate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/borderless_button"
        android:text="@string/generate" />

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#909090" />

    <Button
        android:id="@+id/btn_ViewList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/borderless_button"
        android:text="@string/viewlist" />
</LinearLayout>

</LinearLayout>

your first LinearLayout has the orientation horizontal (so everything will be next to the others), than you add your button bar with the width="match_parent" , so the button bar uses the hold vertical space. 您的第一个LinearLayout具有水平方向(因此所有内容都将位于其他位置附近),而不是使用width="match_parent"添加按钮栏,因此按钮栏使用保持垂直空间。 If you want to put the fragments below eachothers change the orientation of the first to vertical. 如果要将片段放在彼此的下方,请将第一个片段的orientation更改为垂直。 If the fragments shall be next to each other and the button bar below use the following code: (the first LinearLayout is not closed in your code!) 如果这些片段彼此相邻,并且下面的按钮栏使用以下代码:(第一个LinearLayout在您的代码中未关闭!)

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<fragment
    android:id="@+id/mainBodyFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="com.dreamcode.healttracker.MainBodyArea" />

<fragment
    android:id="@+id/subBodyFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="com.dreamcode.healttracker.SubBodyArea" />

<fragment
    android:id="@+id/symptomFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3"
    class="com.dreamcode.healttracker.Symptom" />
</LinearLayout> <!-- added -->
<LinearLayout
    android:id="@+id/footer"
    style="@android:style/ButtonBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#F0F0F0"
    android:orientation="horizontal" >

<Button
    android:id="@+id/btn_Generate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/borderless_button"
    android:text="@string/generate" />

<View
    android:layout_width="1dp"
    android:layout_height="fill_parent"
    android:background="#909090" />

<Button
    android:id="@+id/btn_ViewList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/borderless_button"
    android:text="@string/viewlist" />
</LinearLayout>
</LinearLayout> 

This should do the trick: 这应该可以解决问题:

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

    <LinearLayout
        android:id="@+id/footer"
        style="@android:style/ButtonBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#F0F0F0"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_Generate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/borderless_button"
            android:text="@string/generate" />

        <View
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="#909090" />

        <Button
            android:id="@+id/btn_ViewList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/borderless_button"
            android:text="@string/viewlist" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/footer"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <fragment
            android:id="@+id/mainBodyFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            class="com.dreamcode.healttracker.MainBodyArea" />

        <fragment
            android:id="@+id/subBodyFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            class="com.dreamcode.healttracker.SubBodyArea" />

        <fragment
            android:id="@+id/symptomFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            class="com.dreamcode.healttracker.Symptom" />
    </LinearLayout>

</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM