带有scrollview的android自定义对话框将按钮从屏幕上移开

Cha*_* Ma 8 layout android

我有一个自定义对话框,其布局如下:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mylayout">
   <LinearLayout
       android:id="@+id/title"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">
       <TextView ... />
       <TextView .../>
    </LinearLayout>
    <ScrollView
       android:id="@+id/scrollView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@+id/title">
       <LinearLayout ...>
           <TextView ...
              android:text="lots of text........" />
           <TextView .../>
       </LinearLayout>
     </ScrollView
  <RelativeLayout ...
     android:layout_below="@+id/scrollView">
     <Button ...>
     <Button ...>
  </RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我的问题是滚动视图是当滚动视图中的文本太多时,下面的按钮会从对话框中向下推.我已经能够通过使用android:layout_alignParentBottom ="true"将包含按钮的RelativeLayout锚定到底部来阻止它,但是当滚动视图中的文本较少时,我将整个对话框拉伸到屏幕底部我不希望这样.

我怎样才能获得这样的布局:

[SOME TEXT]
[Scrollable region]
[Buttons]
Run Code Online (Sandbox Code Playgroud)

Gli*_*gor 6

尝试LinearLayout而不是RelativeLayout将按钮放在单独的布局中,并在按钮布局上加权.

LinearLayout - top level start, android:layout_weight="0"
LinearLayout with TextViews embeded, android:layout_weight="1"
LinearLayout with ScrollView embeded, android:layout_weight="1"
LinearLayout with Buttons embeded, android:layout_weight="1"
LinearLayout - top level finish, android:layout_weight="0"
Run Code Online (Sandbox Code Playgroud)

  • 奇怪的是,它的工作原理是将标题布局权重设置为0,滚动视图布局权重设置为1,按钮布局权重设置为0 (12认同)
  • 确实如此.重量越多,它在屏幕上的偏好就越大;)很高兴现在可以使用! (2认同)