【问题标题】:Android ScrollView Take whole device Space (Vertical)Android ScrollView 占用整个设备空间(垂直)
【发布时间】:2017-02-02 19:01:38
【问题描述】:

我的片段 (FrameLayout) (w: match_parent, h: fill_parent) 有两个包含布局:

  1. ScrollView(w:match_parent,h:fill_parent
  2. LinearLayout(w:match_parent,h:wrap_content

我不明白,为什么 LinearLayout 只是覆盖了 ScrollView。我想让ScrollView 停在LinearLayout 的拐角处

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.my.stackoverflow.example.problemFragment">
    <!-- This is my Scrollview with an dynamic nums of TxtViews (Added by onViewCreated in my Fragment) -->
    <ScrollView
        android:id="@+id/ScrollViewId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top">
        <LinearLayout
            android:id="@+id/MessagesId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>
    <!-- Following a LinearLayout wich containing an EditBox and a Button)
    Take a look at the bg color, maybe helpful for you?-->
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/colorWhite"
        android:layout_gravity="bottom"
        android:gravity="bottom">

        <EditText
            android:id="@+id/MessageEditText"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:hint="Enter Message"
            android:inputType="textAutoComplete"
            android:textSize="20sp"
            android:background="@color/colorWhite"
            android:layout_marginLeft="5dp"
            android:paddingTop="12dp"
            android:paddingBottom="12dp"
            android:paddingLeft="12dp"
            android:layout_weight="1"/>
        <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center">
            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginRight="10dp"
                android:foregroundGravity="center"
                android:background="@drawable/circle"/>
            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="5dp"
                android:tint="@color/colorWhite"
                android:background="@drawable/ic_send_black_24dp"/>
        </RelativeLayout>
    </LinearLayout>

素描:

我的解决方案: 我已将 android:layout_marginBottom="50dp" 添加到我的 ScrollView 并将我的 LinearLayout 高度从 wrap_content 设置为 android:layout_height="50dp" 我认为这不是一个干净的解决方案,但它对我有用。

【问题讨论】:

  • Altought 这可能无法解决您的问题:fill_parent 已被弃用。请改用 match_parent。另外:发布您的活动 xml 文件,以便我们为您提供帮助
  • 我已经更新了我的问题

标签: android android-layout scrollview android-linearlayout


【解决方案1】:

如果显示在底部的文本是 TextView,则可以使用: LayoutBelow 并将其放在底部布局的 ScrollView 和 Layout 之下。

 <SrollView
   some height
   some width/>

  <TextView
      layoutBelow = scrollView
       layoutAbove = LinearLayout
  />

 <LinearLayout
    layout with buttons
  />

【讨论】:

  • 我的意思是 TextView 是 ScrollView 的子项。 TextViews的数量是动态的
【解决方案2】:

这应该可行:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context="com.wembleystudios.engagementkit.service.socialbetting.view.activities.SignUpActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignTop="@+id/layout"
    >

</ScrollView>

<LinearLayout
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:id="@+id/layout"
    android:background="@color/black"
    android:orientation="horizontal"></LinearLayout>

【讨论】:

  • 它对我不起作用。我的 ScrollView 内容不再显示在设备上。 LinearLayout 仍然在底部(很好),但正如我所说,我的滚动视图现在是“空的”
  • 问题是您的 Root 布局必须是 RelativeLayout 才能使用我的响应。然后将您的线性布局附加到根的底部,并将滚动视图附加到线性的顶部。
  • Hmpf 这是一个问题,因为我用一个 Activity 构建我的应用程序。每个“页面”都是一个片段。所以,Root Layout 必须是一个RelativeLayout(?)。现在,我刚刚将您的 RelativeLayout 添加为我的 FrameLayout 的第一个子项和所有其他项的父项。这是我的粘贴箱:pastebin.com/LSYg3Adr
【解决方案3】:

您可以通过将根 FrameLayout 替换为任一来实现所需的布局

  1. LinearLayoutlayout_weight

    <LinearLayout
        android:orientation="vertical">
    
        <ScrollView
            android:id="@+id/ScrollViewId"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </ScrollView>
    
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
        </LinearLayout>
    </LinearLayout>
    

    此选项非常简单,您可以轻松地在布局的顶部或底部添加更多元素。缺点是布局必须执行两次。

  2. RelativeLayoutlayout_above

    <RelativeLayout>
    
        <ScrollView
            android:id="@+id/ScrollViewId"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/bottom_bar">
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/bottom_bar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
        </LinearLayout>
    </RelativeLayout>
    

    在向布局添加更多元素时,此选项最为灵活。布局也必须执行两次。

  3. FrameLayout 和在dimens.xml 中定义的固定底部栏高度

    <FrameLayout>
    
        <ScrollView
            android:id="@+id/ScrollViewId"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/bottom_bar_height">
        </ScrollView>
    
        <LinearLayout
            android:layout_height="@dimen/bottom_bar_height"
            android:layout_width="match_parent">
        </LinearLayout>
    </FrameLayout>
    

    此选项与您的解决方案类似,但应定义维度。这不是您想的那么肮脏的解决方案,只是在向布局中添加更多元素或编辑底栏布局时不是很灵活。但性能要好得多,因为布局只执行一次。

【讨论】:

    猜你喜欢
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2021-04-23
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多