Android 使底部工具栏不会移动到布局顶部

Android making bottom tool bar not move to top of layout

这是 的延续,我想在离线时在屏幕底部(底部工具栏下方)收到警告文本。在离线模式下看起来正确:

然而,当我隐藏离线模式时,它会弹出到布局的顶部,就像这样(隐藏我想显示的所有内容):

我尝试将底部工具栏设置为 android:layout_alignParentBottom="true",当我隐藏其中包含 TextView 的 RelativeLayout(离线模式)时,它确实不会弹出到顶部,但是当我隐藏它们时它们会相互重叠不要隐藏离线模式。

对于 Android UI 比我更有经验的人来说可能很容易。XML 布局目前看起来像这样:

<RelativeLayout
    android:id="@+id/mainLyt"
    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">

    <ScrollView 
        android:id="@+id/formScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/bottomBar" >
        <LinearLayout
            android:id="@+id/formLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingTop="15dp"
            android:paddingRight="10dp"
            android:paddingBottom="15dp"
            android:orientation="vertical" >
        </LinearLayout>

    </ScrollView>

    <RelativeLayout
        android:id="@+id/bottomBar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_above="@+id/bottomOffline" 
        android:background="@color/form_toolbar">

        <ImageButton
            android:id="@+id/btnPrev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="btnPrevClicked"
            android:layout_alignParentLeft="true"
            android:focusableInTouchMode="false"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/toolbar_prev"
            android:padding ="8dp"
            />

        <ImageButton
            android:id="@+id/btnIndex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btnPrev"
            android:onClick="btnIndexClicked"
            android:focusableInTouchMode="false"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/toolbar_index"
            android:padding ="8dp"
            />

        <ImageButton
            android:id="@+id/btnValidation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btnIndex"
            android:onClick="btnValidationClicked"
            android:focusableInTouchMode="false"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/toolbar_validate"
            android:padding ="8dp"
            />

        <ImageButton
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="btnNextClicked"
            android:focusableInTouchMode="false"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/toolbar_next"
            android:padding ="8dp"
            />
    <!-- Some Buttons -->
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/bottomOffline"
        android:layout_width="match_parent"
        android:layout_height="34dp"
        android:layout_alignParentBottom="true"
        android:background="@color/orangelight"
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/offline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="false"
            android:text="OFFLINE MODE"
            android:textStyle="bold"
            android:textColor="@color/white"
            android:padding ="8dp"
            />
    </RelativeLayout>

</RelativeLayout>

谢谢!

使用 Java,我们可以通过编程方式将视图设置为在您处于在线模式时在底部对齐。以下内容应该放在您用来删除离线栏的代码之后。 (因为你没有 post 你的 Java,我不能更具体)我假设变量 bottomBarRelativeLayout 类型并通过 findViewById(R.id.bottomBar.

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)bottomBar.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bottomBar.setLayoutParams(params);

这应该做的是在底部栏上设置 ALIGN_PARENT_BOTTOM 标志(XML 中的 android:layout_alignParentBottom="true"),这样它就会留在底部,即使离线栏是删除。因此,如果您在删除离线栏后立即添加它,它将更新视图并正确显示栏

尝试将底部视图包裹在 LinearLayout 中,并从两个嵌套视图中删除对齐字段

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/bottomBar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:background="@color/form_toolbar">

    <ImageButton
        android:id="@+id/btnPrev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btnPrevClicked"
        android:layout_alignParentLeft="true"
        android:focusableInTouchMode="false"
        android:background="?android:attr/selectableItemBackground"
        android:src="@drawable/toolbar_prev"
        android:padding ="8dp"
        />

    <ImageButton
        android:id="@+id/btnIndex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/btnPrev"
        android:onClick="btnIndexClicked"
        android:focusableInTouchMode="false"
        android:background="?android:attr/selectableItemBackground"
        android:src="@drawable/toolbar_index"
        android:padding ="8dp"
        />

    <ImageButton
        android:id="@+id/btnValidation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/btnIndex"
        android:onClick="btnValidationClicked"
        android:focusableInTouchMode="false"
        android:background="?android:attr/selectableItemBackground"
        android:src="@drawable/toolbar_validate"
        android:padding ="8dp"
        />

    <ImageButton
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:onClick="btnNextClicked"
        android:focusableInTouchMode="false"
        android:background="?android:attr/selectableItemBackground"
        android:src="@drawable/toolbar_next"
        android:padding ="8dp"
        />
    <!-- Some Buttons -->
</RelativeLayout>

<RelativeLayout
    android:id="@+id/bottomOffline"
    android:layout_width="match_parent"
    android:layout_height="34dp"
    android:background="@color/orangelight"
    android:gravity="center_horizontal">

    <TextView
        android:id="@+id/offline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="false"
        android:text="OFFLINE MODE"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:padding ="8dp"
        />
</RelativeLayout>