2011-05-19 68 views
16

我试图实现的布局(我认为)非常简单。按钮展开滚动视图

一个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> 

末空的LinearLayout是(不成功)试图阻止视图能扩充到屏幕底部..

在我试过的一切中,无论是按钮都在屏幕的底部,或者当TextView太大时他们会经过它。

这里是什么,我试图做一个素描:enter image description here

+0

这可能帮助,如果你能提供您想要的布局草图。 – 2011-05-19 22:13:23

+0

我认为描述很简单...(第二行) – Matthieu 2011-05-19 22:16:14

回答

23

只是有一个滚动区域,占用了整个页面减去一些按钮在底部吸收量的目标是什么?

如果是这样的话,你可以在外部级别使用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> 
+0

对不起,我猜不太清楚...添加了一个草图... – Matthieu 2011-05-19 22:44:32

+1

嗯,如果将外部LinearLayout设置为wrap_content会发生什么?如果这不起作用,将它包裹在另一个LinearLayout中,在它下面有一个贪婪的(height = 0,weight = 1)不可见的元素? – 2011-05-19 23:15:19

+0

它仍然展开并将按钮从屏幕上移开... – Matthieu 2011-05-19 23:20:22

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> 

希望它可以帮助

+0

和Mayra一样,我想我的描述不够好。我添加了一个草图,使其更加明确。我只想在文本框足够大的时候按钮位于底部... – Matthieu 2011-05-19 22:46:31

+0

并且textView很大?你的按钮将desapears,使用ScrollView作为你所有布局的父母,然后你会确定你的按钮在textView下面,那是你想要的吗? 还有一件事,如果你的textView变得越来越大,这将按钮一次又一次地推到底部,那么scrollView的原因是什么? – Houcine 2011-05-19 22:48:05

+0

我希望按钮始终可见,无论是文本不多的文本字段下方还是文本字段使用所有剩余空间的屏幕底部都可以。如果文本字段是可滚动的,则可能不需要滚动视图,但想法是在需要时滚动文本。 – Matthieu 2011-05-19 22:53:58

1

我发现这个链接真的帮助我。

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

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

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

这里是我的两个方法来处理它:

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); 
} 
+0

经过这更多的工作后,我发现虽然上述答案的作品re是一个更好的解决方案。使用相对布局非常可靠,没有这个答案的所有编程式回转。关键是至少有一个与父顶对齐的元素和一个与父底对齐的元素。其余元素相对于这两个元素对齐,滚动视图最终夹在需要保持静止的任何事物之间。 – alphonzo79 2012-08-13 16:19:44

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>