滚动按钮上方的文本,按钮固定在底部

时间:2011-03-27 06:06:15

标签: android user-interface android-layout

我很难在Android应用中获得我想要的GUI布局结果。

我想要的简要说明:

GUI包括两个TextView和四个Buttons。

四个按钮水平放置,全部在同一行,并固定在屏幕的右下方。

两个TextView中的第一个是从屏幕顶部开始,文本内容从一行到几十行不等 - 超过屏幕适合而不滚动。因此,有时需要滚动来查看所有内容。即使需要滚动,按钮也不参与滚动:它们始终保持固定在屏幕右下角的单行中。当需要滚动时,滚动文本始终位于按钮上方 - 按钮不会覆盖文本。

两个TextView中的第二个显示在第一个TextView的正下方,通常只会在文本的总长度上添加一行或两行。当需要滚动时,第二个TextView将使用第一个TextView滚动,始终显示在第一个TextView的正下方。

其他限制包括我希望所有以下Android设备的布局在垂直和水平屏幕布局中看起来都不错:

  • Android 1.5 API3 QVGA MDPI(320x240)设备
  • Android 1.5 API3 HVGA MDPI设备
  • Android 1.6 API4 QVGA LDPI设备
  • Android 2.3 API10 WVGA800设备
  • 上述设备之间有屏幕的所有内容

我会担心平板电脑的另一天(比如明天)。

-

我尝试了许多不同的布局组合,但还没有任何一个非常接近目标。

(我试过的一些布局组合,我可以使用RelativeLayout修复屏幕左下方的按钮,但是我用scolling文本尝试的所有内容都会导致文本在按钮后滚动 - 按钮覆盖我没想办法让按钮与右下角对齐。)

如果有人帮忙解决这个问题,下面的布局示例xml是一个会话起点,但肯定无法达到目标结果,如下面的屏幕截图所示,使用相同的布局示例xml生成。 (虽然有些屏幕截图显示了同样的问题,但它们有助于显示我在不同屏幕上的位置。)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Text View 1.  Text varies from a few lines to many more lines than what fits on the screen.  Scrolling is necessary to see it all." />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Text View 2.  Short text entry sits below Text View 1." />
        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Button 1" />
        <Button
            android:id="@+id/button_2"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Button 2" />
        <Button
            android:id="@+id/button_3"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Button 3" />
        <Button
            android:id="@+id/button_4"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Button 4" />
    </LinearLayout>
</LinearLayout>

1.5_API3_HVGA_Horizo​​ntal_MDPI - short_text:
1.5_API3_HVGA_Horizontal_MDPI - short_text

问题:按钮应与屏幕的右下角对齐。

-

1.5_API3_HVGA_Vertical_MDPI - short_text:
1.5_API3_HVGA_Vertical_MDPI - short_text

问题:按钮应与屏幕的右下角对齐。

-

1.5_API3_QVGA_240x320_MDPI - short_text:
1.5_API3_QVGA_240x320_MDPI - short_text

问题:第4个按钮被粉碎。首选三个按钮上的文字以便在必要时包装,留出足够的空间来显示第四个按钮。

-

1.5_API3_QVGA_320x240_MDPI - short_text:
1.5_API3_QVGA_320x240_MDPI - short_text

问题:按钮应与屏幕的右下角对齐。

-

1.6_API4_QVGA_Horizo​​ntal_LDPI - long_text:
1.6_API4_QVGA_Horizontal_LDPI - long_text

问题:当文本几乎填满屏幕时,按钮行被粉碎。按钮行不应该被粉碎,应该固定在屏幕的右下角。文本应滚动在按钮上方。

-

1.6_API4_QVGA_Horizo​​ntal_LDPI - short_text:
1.6_API4_QVGA_Horizontal_LDPI - short_text

问题:按钮应与屏幕的右下角对齐。

-

1.6_API4_QVGA_Horizo​​ntal_LDPI - very_long_text,顶部滚动条:
1.6_API4_QVGA_Horizontal_LDPI - very_long_text, scrollbar at top

问题:按钮不在屏幕上。它们应固定在屏幕的右下角。

-

1.6_API4_QVGA_Horizo​​ntal_LDPI - very_long_text,底部滚动条:
1.6_API4_QVGA_Horizontal_LDPI - very_long_text, scrollbar at bottom

问题:虽然文本滚动条位于底部,但按钮无处可寻。它们应固定在屏幕的右下角。

-

1.6_API4_QVGA_Vertical_LDPI - short_text:
1.6_API4_QVGA_Vertical_LDPI - short_text

问题:按钮应与屏幕的右下角对齐。

-

有什么建议吗?

-

其他信息:当我尝试使用RelativeLayout,并使用android:layout_alignParentBottom="true"修复屏幕底部的按钮时,我的问题是我不知道如何修复滚动视图的底部按钮的顶部。使用android:layout_alignBottom="@id/buttons"只是将滚动视图的底部与按钮的底部对齐,但按钮会覆盖文本,如下所示:

-

更新:将按钮固定在右下角,按钮上方滚动文字的问题已解决。

以下是目前为止工作的更改后的布局XML(如果要查看滚动,请将更多文本粘贴到文本视图1中):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Text View 1.  Text varies from a few lines to many more lines than what fits on the screen.  Scrolling is necessary to see it all." />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Text View 2.  Short text entry sits below Text View 1." />
        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right">
        <Button
            android:id="@+id/button_1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Button 1" />
        <Button
            android:id="@+id/button_2"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Button 2" />
        <Button
            android:id="@+id/button_3"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Button 3" />
        <Button
            android:id="@+id/button_4"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Button 4" />
    </LinearLayout>
</LinearLayout>
我还有一个问题,我将发布一个问题新问题。

2 个答案:

答案 0 :(得分:12)

将ScrollView的layout_height设置为0dp并将其layout_weight设置为1.这应该将按钮按到屏幕底部但不能再进一步。

答案 1 :(得分:4)

更改用于RelativeLayout的布局,这将有助于您在屏幕底部附加按钮。

我也同意在可滚动的可滚动长度上使用layout_height =“0dip”和layout_weight =“1”。

修改

我刚刚将代码更改为此,并且在我的HTC Desire上它对我来说很好。它应该适用于您的测试用例。仅使用线性布局结束,但看起来很好。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Text View 1.  Text varies from a few lines to many more lines than what fits on the screen.  Scrolling is necessary to see it all." />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Text View 2.  Short text entry sits below Text View 1." />
        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_1"
            android:layout_height="fill_parent"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:text="Button 1" />
        <Button
            android:id="@+id/button_2"
            android:layout_height="fill_parent"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:text="Button 2" />
        <Button
            android:id="@+id/button_3"
            android:layout_height="fill_parent"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:text="Button 3" />
        <Button
            android:id="@+id/button_4"
            android:layout_height="fill_parent"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:text="Button 4" />
    </LinearLayout>
</LinearLayout>