【问题标题】:Android Scrolling of a ScrollViewScrollView 的 Android 滚动
【发布时间】:2014-10-09 03:23:18
【问题描述】:

在我的布局中,我有一个RelativeLayout, 它的子元素是顶部的ScrollView、屏幕底部的EditTextButton 在 ScrollView 中有一个 LinearLayout 我正在获取一些数据并将其动态添加到 LinearLayout 并使用Scrollview.scrollTO() 方法滚动视图

但问题是滚动视图占据了整个屏幕,即数据也显示在编辑文本和按钮下

有什么办法可以将滚动视图限制在编辑文本和按钮的顶部?

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

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:transcriptMode="alwaysScroll" 
    >

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="461dp"
        android:orientation="vertical" >

    </LinearLayout>


</ScrollView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/editText1"
    android:onClick="check"
    android:text="@string/btnlb" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/button1"
    android:ems="10"
    android:inputType="text" />

</RelativeLayout>

【问题讨论】:

  • android:layout_above="@+id/button1".. 写在scrollview属性中
  • 当我添加这个语句时,它引发了一个异常,说 android.widget.button 不能被强制转换为 android.widget.linearlayout--@ShabbirDhangot

标签: android android-layout layout scrollview


【解决方案1】:

您可以将滚动视图 layout_above 属性设置为按钮或编辑文本的 id。但是,为此,您必须在声明滚动视图的上方声明编辑文本/按钮(我认为?)。例如像这样的东西(我没有测试过)

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/editText1"
    android:onClick="check"
    android:text="@string/btnlb" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/button1"
    android:ems="10"
    android:inputType="text" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/button1"
    android:transcriptMode="alwaysScroll" 
    >

    <LinearLayout
    ....
    </LinearLayout>

</ScrollView>

</RelativeLayout>

注意:scrollview 的高度和宽度应该是 match_parent,而不是 wrap_content

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多