相对布局中的滚动视图

4

我需要在相对布局中添加一个滚动视图。

当我添加了ScrollView,结果如下http://postimg.org/image/6abxth299/,其中组件只占据了屏幕的一半,并且打开此屏幕时键盘会默认弹出。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical" >
    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:layout_centerHorizontal="true"
        android:id="@+id/data_entry_layout">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Post Title"
                android:id="@+id/post_title"
                android:layout_alignBaseline="@+id/post_title_input"
                android:layout_alignBottom="@+id/post_title_input"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:textSize="26px"/>

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/post_title_input"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_toRightOf="@+id/post_title"
                android:layout_toEndOf="@+id/post_title"
                android:inputType="textCapSentences"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Post Description"
                android:id="@+id/post_description"
                android:layout_below="@+id/post_title_input"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="39dp"
                android:textSize="26px"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine|textCapSentences"
                android:ems="10"
                android:id="@+id/editText"
                android:layout_marginTop="16dp"
                android:layout_below="@+id/post_description"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"/>

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imageButton1"
                android:layout_below="@+id/editText"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:baselineAlignBottom="false"
                android:contextClickable="true"
                android:src="@drawable/image_button_icon"
                android:onClick="galleryIntent"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cancel"
                android:id="@+id/cancel"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@id/imageButton1"

               />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Submit"
                android:id="@+id/submit"
                android:layout_alignTop="@+id/cancel"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_below="@id/imageButton1"/>

    </RelativeLayout>
</ScrollView>

在ScrollView中,永远不要使用match_parent作为RelativeLayout的高度,而应该将其改为wrap_content - Dmitri Timofti
改变了它,但是组件仍然占据了屏幕的一半。 - cgeekmt
你想要什么? - Sagar Nayak
图片按钮的高度需要响应式设计,即根据使用设备的不同而变化,以占据整个屏幕。 - cgeekmt
4个回答

1
滚动视图的第一个直接子项的高度,无论是相对布局还是线性布局,都应该使用wrap content而不是match parent。同时,内部子项应该具有特定的权重或高度,而不是像您为imageButton所做的那样匹配parent。

将高度更改为430像素,它可以工作。但是,我认为它对于其他设备不会是响应式的,对吗?(即高度不会相应地调整)? - cgeekmt
你把高度改成了430像素? - D Agrawal
<ImageButton android:layout_width="match_parent" android:layout_height="430px" - cgeekmt

0

我认为在RelativeLayout中ScrollView不起作用,最好使用LinearLayout。


0

尝试在您的AndroidManifest中的此活动中添加以下内容,以便默认情况下删除键盘

android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

对于ScrollView,请使用android:layout_height="wrap_content",建议在此类用途中使用具有垂直方向布局的LinearLayout


0

试试这个...

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:id="@+id/data_entry_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
        <RelativeLayout
            android:id="@+id/Rel12345"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <TextView
            android:id="@+id/post_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/post_title_input"
            android:layout_alignBottom="@+id/post_title_input"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:text="Post Title"
            android:textSize="26px" />

        <EditText
            android:id="@+id/post_title_input"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_toEndOf="@+id/post_title"
            android:layout_toRightOf="@+id/post_title"
            android:inputType="textCapSentences" />

        <TextView
            android:id="@+id/post_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/post_title_input"
            android:layout_marginTop="39dp"
            android:text="Post Description"
            android:textSize="26px" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/post_description"
            android:layout_marginTop="16dp"
            android:ems="10"
            android:inputType="textMultiLine|textCapSentences" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/editText"
            android:baselineAlignBottom="false"
            android:contextClickable="true"
            android:onClick="galleryIntent"
            />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/Rel1234"
            android:layout_below="@id/Rel12345"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">

            <Button
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:text="Cancel"/>

            <Button
                android:id="@+id/submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@+id/cancel"
                android:text="Submit" />
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

尝试将以下内容添加到AndroidManifest中,以删除默认情况下出现的键盘

android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

@cgeekmt 这是经过测试的完整解决方案,可以解决你的问题。 - Saurabh Vardani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接