下面的按钮扩展scrollview

时间:2011-05-19 22:03:08

标签: android user-interface android-layout

我尝试实现的布局(我认为)非常简单。

根据内部内容扩展一个TextView。下面是两个按钮(确定/取消)。

我可以让按钮贴在屏幕的底部,并且工作正常。但我想在TextView下面放置这些按钮。 我尝试了很多不同的东西,但这是我最新的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <ScrollView android:id="@+id/Scroll"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
            >
        <TextView
                android:id="@+id/Text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
    </ScrollView>
    <RelativeLayout android:id="@+id/buttons"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"
                    android:layout_below="@+id/Scroll"
                    android:layout_above="@+id/bottom"
            >
        <LinearLayout
                android:orientation="horizontal"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent">
            <Button android:id="@+id/ButtonOK"
                    android:layout_height="80dp"
                    android:layout_width="fill_parent"
                    android:text="OK"
                    android:layout_weight="1"/>
            <Button android:id="@+id/ButtonCancel"
                    android:layout_height="80dp"
                    android:layout_width="fill_parent"
                    android:text="Cancel"
                    android:layout_weight="1"/>
        </LinearLayout>
    </RelativeLayout>
    <LinearLayout android:id="@+id/bottom"
                  android:layout_height="0px"
                  android:layout_width="fill_parent"
                  android:layout_alignParentBottom="true"/>
</RelativeLayout>

最后的空线性布局是一次(不成功)尝试阻止视图延伸到屏幕底部...

在我尝试过的所有内容中,按钮都位于屏幕的底部,或者当TextView太大时它们会超过它。

以下是我想要做的草图:enter image description here

5 个答案:

答案 0 :(得分:24)

目标是否只有一个可滚动区域占据整个页面减去底部某些按钮占用的数量?

如果是这种情况,您可以在外层使用LinearLayout并使用权重:

<LinearLayout 
   android:layout_height="wrap_content" 
   orientation="vertical"...>
   <ScrollView
      android:layout_height="0dp"
      android:layout_weight="1"
      .../>
   <LayoutWithButtons
      android:layout_height="wrap_content"
      ..../>
</LinearLayout>

答案 1 :(得分:1)

这应该是xml中的答案

android:fillViewport="true"

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

答案 2 :(得分:1)

我发现这个链接对我有帮助。

http://www.droidnova.com/layout-technique-static-elements-below-scrollview,123.html

我对键盘解决方案有一些非常具体的定制需求 - 要求我自己处理键盘的显示和拆卸,作为frameLayout中的z层元素。我使用该链接中的解决方案来完成该操作并同时更改scrollView的大小。滚动视图中的按钮从与屏幕底部齐平移动,以与键盘顶部齐平。

关键是在scrollView上有android:fillViewport =“true”。然后以编程方式将scrollView的下边距设置为和底部元素的上边距为-height。

以下是我处理它的两种方法:

private void setMargins_noKeyboard()
{
    ScrollView fieldsScroller = (ScrollView)findViewById(R.id.scroll_fields_view);
    FrameLayout keyboardFrame = (FrameLayout)findViewById(R.id.keyboard_root);
    int buttonsKeyboardSize = keyboardFrame.getHeight();

    MarginLayoutParams frameParams = (MarginLayoutParams)keyboardFrame.getLayoutParams();
    frameParams.setMargins(0, 0, 0, 0);

    MarginLayoutParams scrollParams = (MarginLayoutParams)fieldsScroller.getLayoutParams();
    scrollParams.setMargins(0, 0, 0, 0);

    keyboardFrame.setLayoutParams(frameParams);
    fieldsScroller.setLayoutParams(scrollParams);
}

private void setMargins_keyboard()
{
    ScrollView fieldsScroller = (ScrollView)findViewById(R.id.scroll_fields_view);
    FrameLayout keyboardFrame = (FrameLayout)findViewById(R.id.keyboard_root);
    int buttonsKeyboardSize = keyboardFrame.getHeight();

    MarginLayoutParams frameParams = (MarginLayoutParams)keyboardFrame.getLayoutParams();
    frameParams.setMargins(0, -buttonsKeyboardSize, 0, 0);

    MarginLayoutParams scrollParams = (MarginLayoutParams)fieldsScroller.getLayoutParams();
    scrollParams.setMargins(0, 0, 0, buttonsKeyboardSize);

    keyboardFrame.setLayoutParams(frameParams);
    fieldsScroller.setLayoutParams(scrollParams);
}

答案 3 :(得分:0)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >
    <RelativeLayout android:id="@+id/buttons"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_alignParentBottom="true"
        >
    <LinearLayout
            android:orientation="horizontal"
            android:layout_alignParentTop="true"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">
        <Button android:id="@+id/ButtonOK"
                android:layout_height="80dp"
                android:layout_width="fill_parent"
                android:text="OK"
                android:layout_weight="1"/>
        <Button android:id="@+id/ButtonCancel"
                android:layout_height="80dp"
                android:layout_width="fill_parent"
                android:text="Cancel"
                android:layout_weight="1"/>
    </LinearLayout>
</RelativeLayout>
<ScrollView android:id="@+id/ScrollParent"
            android:layout_above="@id/buttons"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        >

<ScrollView android:id="@+id/Scroll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
        >
    <TextView
            android:id="@+id/Text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
</ScrollView>
</RelativeLayout>

希望有所帮助

答案 4 :(得分:0)

这个xml适用于我的应用:

<?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" >

     <TextView
        android:id="@+id/nameOfrecipe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:layout_marginLeft="10sp"
        android:layout_alignParentTop="true"
        android:textSize="20sp"
        android:text="Name of ingredients" />   

    <TableLayout
        android:id="@+id/tableOfingredients"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/nameOfrecipe"
        android:layout_marginTop="40sp"
        android:layout_marginLeft="10sp"
        android:layout_marginRight="10sp" >
    </TableLayout>
    <RelativeLayout android:id="@+id/l"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_below="@id/tableOfingredients"
        >                     
    <Button
        android:id="@+id/addDirectionsB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"     
        android:text="Add..." />

    <Button
        android:id="@+id/cancelDirectionsB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"   
        android:layout_toRightOf="@id/addDirectionsB"
        android:text="Cancel" />

       <ScrollView 
        android:id="@+id/directionsScroll"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/addDirectionsB">        
            <EditText 
                android:id="@+id/directionsET"       
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine" >
                <requestFocus />
            </EditText>
    </ScrollView>
    </RelativeLayout>   
</RelativeLayout>
相关问题