如何在Android中将按钮放在滚动视图下方并始终显示按钮而不被滚动视图重叠?

lit*_*hen 0 xml android android-layout

我的布局是这样的

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

    <EditText
        android:id="@+id/searchKey"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="xx" />

    <ScrollView
        android:layout_weight="1"
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/search_results"
            android:textSize="@dimen/text_font_size"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </ScrollView>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_gravity="center|bottom"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/lastCharacterButton"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lastChar_btn" />

        <Button
            android:id="@+id/nextCharacterButton"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/nextChar_btn" />

    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

但是,当文本很少或没有文本时,按钮会被拉起,当 search_results TextView 中有太多文本时,按钮将不会显示,因为滚动视图将空间一直占据到底部。有什么建议让按钮始终保持在底部并始终可见吗?

Rob*_*Rob 5

首先,您必须将 ScrollView 的高度设置为 0dp。

<ScrollView
    android:layout_weight="1"
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="0dp">
    <TextView
        android:id="@+id/search_results"
        android:textSize="@dimen/text_font_size"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

您必须这样做,因为权重参数将根据可用空间的大小来控制视图的大小。如果将其设置为包裹内容,则需要多少空间来包裹所有内容。

然后你需要删除 LinearLayout 的权重参数:

<LinearLayout
            android:orientation="horizontal"
            android:layout_gravity="center|bottom"
            android:id="@+id/LL1"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/lastCharacterButton"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Last_Char" />

            <Button
                android:id="@+id/nextCharacterButton"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="next_char" />

        </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这样做的原因与前面的解释正好相反:在这种情况下,换行内容将占用始终显示其中的按钮所需的空间。