简体   繁体   English

ScrollView在Android中落后于按钮

[英]ScrollView going behind button in Android

Why is the scrollview going behind the button here? 为什么滚动视图位于此处的按钮后面? I want the scrollview to stretch in height depending on the device but cant get it to work as it always goes behind the button. 我希望scrollview的高度取决于设备,但无法使其正常工作,因为它始终位于按钮后面。

在此处输入图片说明

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail_view"
    style="@style/DetailView">

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

        <TextView
            style="@style/TextView"
            android:id="@+id/textview_message_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
        </TextView>

         <TextView
            style="@style/TextView"
            android:id="@+id/textview_message_date"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
        </TextView>

        <ScrollView 
            android:id="@+id/scrollview_message_text" 
            android:layout_height="wrap_content" 
            android:paddingBottom="20dip"
            android:layout_width="fill_parent"> 
             <TextView
                style="@style/TextView"
                android:id="@+id/textview_message_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </TextView>
        </ScrollView>

    </LinearLayout>

     <LinearLayout style="@style/DetailView.Buttons"
          android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:orientation="vertical">

         <Button
            style="@style/Button"
            android:layout_marginTop="5dip"
            android:id="@+id/button_view_file"
            android:text="@string/viewfile"/>

    </LinearLayout>

</RelativeLayout>

This happens because you use Relative layout as the root layout. 发生这种情况是因为您使用相对布局作为根布局。 So both layout are placed on the same place but the button layout is declared after the scrollview so it is shown above the scrollview The opposite would happen if you were defining the button before the scrollview 因此,两个布局都放置在同一位置,但是按钮布局在滚动视图之后声明,因此显示在滚动视图上方。如果您在滚动视图之前定义按钮,则会发生相反的情况

So you should define it as shown below to achieve what you want. 因此,您应该按如下所示定义它,以实现所需的功能。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail_view"
    style="@style/DetailView">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_above="@id/layout_bottom"
        android:orientation="vertical" >

        <TextView
            style="@style/TextView"
            android:id="@+id/textview_message_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
        </TextView>

         <TextView
            style="@style/TextView"
            android:id="@+id/textview_message_date"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
        </TextView>

        <ScrollView 
            android:id="@+id/scrollview_message_text" 
            android:layout_height="wrap_content" 
            android:paddingBottom="20dip"
            android:layout_width="fill_parent"> 
             <TextView
                style="@style/TextView"
                android:id="@+id/textview_message_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </TextView>
        </ScrollView>

    </LinearLayout>

     <LinearLayout 
          android:id="@+id/layout_bottom"
         style="@style/DetailView.Buttons"
          android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:orientation="vertical">

         <Button
            style="@style/Button"
            android:layout_marginTop="5dip"
            android:id="@+id/button_view_file"
            android:text="@string/viewfile"/>

    </LinearLayout>

</RelativeLayout>

or you can try and use LinearLayout as the root view and set 或者您可以尝试使用LinearLayout作为根视图并进行设置

layout_height = "0dp" 
layout_weight = "1" 

to the layout containing the scrollview 到包含滚动视图的布局

using both these way will make the button stick to the bottom of the layout and the top layout will get all the remaining height. 同时使用这两种方法会使按钮停留在布局的底部,而顶部布局将获得所有剩余的高度。

Good Luck 祝好运

Here is a working copy of what I think you are looking for 这是我认为您正在寻找的工作副本

1: Moved the buttons to the top of the relative layout ( added an id ) android:layout_alignParentBottom="true" 1:将按钮移至相对布局的顶部(添加了id)android:layout_alignParentBottom =“ true”

 <LinearLayout android:id="@+id/buttons"
              android:layout_alignParentBottom="true"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

       <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="5dip"
            android:id="@+id/button_view_file"
            android:text="Button"/>

   </LinearLayout>

2: Add in the layout containing the scroll change the width and height to match parent but ( CRITICAL ) add android:layout_above="@id/buttons" 2:添加包含滚动的布局,更改宽度和高度以匹配父级,但(CRITICAL)添加android:layout_above =“ @ id / buttons”

  <LinearLayout
        android:layout_above="@id/buttons"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:orientation="vertical" >

So putting it all together: 所以放在一起:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/detail_view"
            >


  <!-- Buttons at the top of layout but aligned to the bottom -->
  <LinearLayout android:id="@+id/buttons"
              android:layout_alignParentBottom="true"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="5dip"
            android:id="@+id/button_view_file"
            android:text="Button"/>

 </LinearLayout>


 <!-- Linear layout to fill screen, but assigned to layout above the buttons -->
 <LinearLayout
        android:layout_above="@id/buttons"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:orientation="vertical" >

     <TextView
             android:id="@+id/textview_message_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
     </TextView>

     <TextView
            android:id="@+id/textview_message_date"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
     </TextView>

     <ScrollView
            android:id="@+id/scrollview_message_text"
            android:layout_height="wrap_content"
            android:paddingBottom="20dip"
            android:layout_width="fill_parent">
        <TextView
                android:id="@+id/textview_message_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
        </TextView>
    </ScrollView>

 </LinearLayout>



</RelativeLayout>

I removed the styles so I could see it in the layout an make sure this worked before posting. 我删除了样式,以便可以在布局中看到它,并确保在发布之前可以正常工作。 Hope this helps. 希望这可以帮助。

Relative layout allows you to layer elements on top of each other. 相对布局允许您将元素彼此层叠。 So if that is not what you are looking for you need to make use of the toLeft, toRight, above, below directives to nudge the layouts into the correct position. 因此,如果这不是您要查找的内容,则需要使用上面,下面的toLeft,toRight,to指令来将布局微调到正确的位置。

Why is the scrollview going behind the button here? 为什么滚动视图位于此处的按钮后面?

Because it is coming first in your layout, ReletiveLayout and FrameLayout can be used to provide Z-ordering, So every child which comes later in your layout having higher z value so it goes up.Add android:layout_below = "@id/scrollview_message_text" to your last linearlayout or order your views within in mind of this characteristic of reletivelayout. 由于它在布局中ReletiveLayout在首位,因此ReletiveLayoutFrameLayout可用于提供Z顺序,因此,布局中排在后面的每个子项都具有较高的z值,因此它会上升。添加android:layout_below = "@id/scrollview_message_text"考虑到reletivelayout的这一特性,请选择最后一个linearlayout或按顺序排列视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM