【问题标题】:Android scroll view does not work well fits on the screen when it should notAndroid 滚动视图在不应该的情况下不能很好地适应屏幕
【发布时间】:2014-11-26 11:44:27
【问题描述】:

我的项目是创建 email 字段 dynamic ,从某种意义上说,我可以有一个、两个和其他。所以我想把整个右侧的细节放在 ScrollView 中。问题是 scoolview 永远不会出现,不可滚动并且无法找出问题所在,我不希望它适合屏幕,但您可以滚动。有人能帮我吗 ?非常感谢

ma​​in.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:orientation="horizontal"
        android:weightSum="10" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:orientation="vertical"
            android:weightSum="10" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:scaleType="fitStart"
                android:src="@drawable/user" />
        </LinearLayout>

        <ScrollView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="7" 
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="21" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="4"
                    android:orientation="horizontal"
                    android:weightSum="10" >

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="5"
                        android:orientation="vertical"
                        android:weightSum="10" >

                        <TextView
                            android:id="@+id/labelname"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="3"
                            android:background="@color/gray"
                            android:drawableLeft="@drawable/ic_action_person"
                            android:gravity="left|center_vertical"
                            android:text=" Name"
                            android:textSize="@dimen/campaign_textfontsize_label" />

                        <EditText
                            android:id="@+id/name"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="7"
                            android:background="@drawable/edit_text"
                            android:gravity="center_vertical"
                            android:hint="Nome"
                            android:imeOptions="actionDone"
                            android:inputType="textCapSentences|textAutoCorrect"
                            android:singleLine="true"
                            android:textSize="@dimen/campaign_textfontsize" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="5"
                        android:orientation="vertical"
                        android:weightSum="10" >

                        <TextView
                            android:id="@+id/labelsurname"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="3"
                            android:background="@color/gray"
                            android:drawableLeft="@drawable/ic_action_person"
                            android:gravity="left|center_vertical"
                            android:text=" Surname"
                            android:textSize="@dimen/campaign_textfontsize_label" />

                        <EditText
                            android:id="@+id/surname"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="7"
                            android:background="@drawable/edit_text"
                            android:gravity="center_vertical"
                            android:hint="Surname"
                            android:imeOptions="actionDone"
                            android:inputType="textCapSentences|textAutoCorrect"
                            android:singleLine="true"
                            android:textSize="@dimen/campaign_textfontsize" />
                    </LinearLayout>
                </LinearLayout>

                <TextView
                    android:id="@+id/labelemail"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:background="@color/gray"
                    android:drawableLeft="@drawable/ic_action_email"
                    android:gravity="left|center_vertical"
                    android:text=" E-Mail"
                    android:textSize="@dimen/campaign_textfontsize_label" />

                <EditText
                    android:id="@+id/emailHome"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="3"
                    android:background="@drawable/edit_text"
                    android:gravity="center_vertical"
                    android:hint="E-Mail"
                    android:imeOptions="actionDone"
                    android:inputType="textCapSentences|textAutoCorrect"
                    android:singleLine="true"
                    android:textSize="@dimen/campaign_textfontsize" />

                <EditText
                    android:id="@+id/emailWork"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="3"
                    android:background="@drawable/edit_text"
                    android:gravity="center_vertical"
                    android:hint="E-Mail"
                    android:imeOptions="actionDone"
                    android:inputType="textCapSentences|textAutoCorrect"
                    android:singleLine="true"
                    android:textSize="@dimen/campaign_textfontsize" />

                <EditText
                    android:id="@+id/emailOther"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="3"
                    android:background="@drawable/edit_text"
                    android:gravity="center_vertical"
                    android:hint="E-Mail"
                    android:imeOptions="actionDone"
                    android:inputType="textCapSentences|textAutoCorrect"
                    android:singleLine="true"
                    android:textSize="@dimen/campaign_textfontsize" />

                <EditText
                    android:id="@+id/emailMobile"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="3"
                    android:background="@drawable/edit_text"
                    android:gravity="center_vertical"
                    android:hint="E-Mail"
                    android:imeOptions="actionDone"
                    android:inputType="textCapSentences|textAutoCorrect"
                    android:singleLine="true"
                    android:textSize="@dimen/campaign_textfontsize" />

                <TextView
                    android:id="@+id/labelnumber"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:background="@color/gray"
                    android:drawableLeft="@drawable/ic_action_dial_pad"
                    android:gravity="left|center_vertical"
                    android:text=" Telefono"
                    android:textSize="@dimen/campaign_textfontsize_label" />

                <EditText
                    android:id="@+id/number"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="3"
                    android:background="@drawable/edit_text"
                    android:focusableInTouchMode="true"
                    android:gravity="center_vertical"
                    android:hint="Number"
                    android:imeOptions="actionDone"
                    android:inputType="phone"
                    android:singleLine="true"
                    android:textSize="@dimen/campaign_textfontsize" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="10" >

        <Button
            android:id="@+id/save"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="@drawable/color_button"
            android:clickable="true"
            android:drawableStart="@drawable/ic_action_save"
            android:focusable="true"
            android:text="Save"
            android:textColor="#78858B"
            android:textStyle="bold" />

        <Button
            android:id="@+id/delete"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="@drawable/color_button"
            android:clickable="true"
            android:drawableStart="@drawable/ic_action_discard"
            android:focusable="true"
            android:text="Delete"
            android:textColor="#78858B"
            android:textStyle="bold" />

        <Button
            android:id="@+id/cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="@drawable/color_button"
            android:clickable="true"
            android:drawableStart="@drawable/ic_action_cancel"
            android:focusable="true"
            android:text="Cancel"
            android:textColor="#78858B"
            android:textStyle="bold" />

        <Button
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/color_button"
            android:clickable="true"
            android:drawableStart="@drawable/ic_action_add_person"
            android:focusable="true"
            android:textColor="#78858B"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 嘿,将你的滚动视图高度设置为某个静态值,这样它就会准确地显示你的高度,而且它也是可滚动的。
  • 我尝试以 700dp 为例,正常但不滚动,保持固定
  • 意味着700dp是你所有的内容都可以看到吗?将您的高度设置为 100 dp,然后检查它。
  • 不起作用,我认为放置固定大小对我没有用,因为 ScrollView 有重量,然后您必须适应我使用的任何设备上的页面

标签: android xml scrollview android-linearlayout android-layout-weight


【解决方案1】:
<ScrollView 
         android:layout_width="match_parent"
          android:layout_height="wrap_content"
        >
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/feedback_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:ems="10"
        android:gravity="center"
        android:hint="Your Feedback"
        android:inputType="textMultiLine"
        android:padding="5dp"
        android:background="@drawable/edittext_feedback"
        android:textColor="#666"
        android:typeface="serif" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/feedback_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:ems="10"
        android:gravity="center"
        android:hint="Your Email Address"
        android:inputType="textEmailAddress"
        android:padding="5dp"
        android:textColor="#666"
        android:typeface="serif"
        android:background="@drawable/edittext_feedback"
         />

    <Button
        android:id="@+id/feedback_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:background="@drawable/button_shape"
        android:padding="15dp"
        android:text="Submit Feedback"
        android:textColor="#666"
        android:typeface="monospace" />
</LinearLayout>
</ScrollView>

查看此代码并分析它是否对您有帮助。而且滚动视图必须有一个孩子。

【讨论】:

  • 滚动视图必须有权重
【解决方案2】:

尝试仅将 ScrollView 用于各种电子邮件 EditText 并赋予 ScrollView 权重,以便显示多个电子邮件 EditText 需要休息空间:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.90"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.30"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:scaleType="fitStart"
                android:src="@drawable/user" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.70"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/labelname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/gray"
                        android:drawableLeft="@drawable/ic_action_person"
                        android:gravity="left|center_vertical"
                        android:text=" Name"
                        android:textSize="@dimen/campaign_textfontsize_label" />

                    <EditText
                        android:id="@+id/name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/edit_text"
                        android:gravity="center_vertical"
                        android:hint="Nome"
                        android:imeOptions="actionDone"
                        android:inputType="textCapSentences|textAutoCorrect"
                        android:singleLine="true"
                        android:textSize="@dimen/campaign_textfontsize" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/labelsurname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/gray"
                        android:drawableLeft="@drawable/ic_action_person"
                        android:gravity="left|center_vertical"
                        android:text=" Surname"
                        android:textSize="@dimen/campaign_textfontsize_label" />

                    <EditText
                        android:id="@+id/surname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/edit_text"
                        android:gravity="center_vertical"
                        android:hint="Surname"
                        android:imeOptions="actionDone"
                        android:inputType="textCapSentences|textAutoCorrect"
                        android:singleLine="true"
                        android:textSize="@dimen/campaign_textfontsize" />
                </LinearLayout>
            </LinearLayout>

            <TextView
                android:id="@+id/labelemail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/gray"
                android:drawableLeft="@drawable/ic_action_email"
                android:gravity="left|center_vertical"
                android:text=" E-Mail"
                android:textSize="@dimen/campaign_textfontsize_label" />

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fillViewport="true">

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

                    <EditText
                        android:id="@+id/emailHome"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/edit_text"
                        android:gravity="center_vertical"
                        android:hint="E-Mail"
                        android:imeOptions="actionDone"
                        android:inputType="textCapSentences|textAutoCorrect"
                        android:singleLine="true"
                        android:textSize="@dimen/campaign_textfontsize" />

                    <EditText
                        android:id="@+id/emailWork"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/edit_text"
                        android:gravity="center_vertical"
                        android:hint="E-Mail"
                        android:imeOptions="actionDone"
                        android:inputType="textCapSentences|textAutoCorrect"
                        android:singleLine="true"
                        android:textSize="@dimen/campaign_textfontsize" />

                    <EditText
                        android:id="@+id/emailOther"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/edit_text"
                        android:gravity="center_vertical"
                        android:hint="E-Mail"
                        android:imeOptions="actionDone"
                        android:inputType="textCapSentences|textAutoCorrect"
                        android:singleLine="true"
                        android:textSize="@dimen/campaign_textfontsize" />

                    <EditText
                        android:id="@+id/emailMobile"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/edit_text"
                        android:gravity="center_vertical"
                        android:hint="E-Mail"
                        android:imeOptions="actionDone"
                        android:inputType="textCapSentences|textAutoCorrect"
                        android:singleLine="true"
                        android:textSize="@dimen/campaign_textfontsize" />
                </LinearLayout>
            </ScrollView>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/labelnumber"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/gray"
                    android:drawableLeft="@drawable/ic_action_dial_pad"
                    android:gravity="left|center_vertical"
                    android:text=" Telefono"
                    android:textSize="@dimen/campaign_textfontsize_label" />

                <EditText
                    android:id="@+id/number"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/edit_text"
                    android:focusableInTouchMode="true"
                    android:gravity="center_vertical"
                    android:hint="Number"
                    android:imeOptions="actionDone"
                    android:inputType="phone"
                    android:singleLine="true"
                    android:textSize="@dimen/campaign_textfontsize" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.10"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/save"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/color_button"
            android:clickable="true"
            android:drawableStart="@drawable/ic_action_save"
            android:focusable="true"
            android:text="Save"
            android:textColor="#78858B"
            android:textStyle="bold" />

        <Button
            android:id="@+id/delete"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/color_button"
            android:clickable="true"
            android:drawableStart="@drawable/ic_action_discard"
            android:focusable="true"
            android:text="Delete"
            android:textColor="#78858B"
            android:textStyle="bold" />

        <Button
            android:id="@+id/cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/color_button"
            android:clickable="true"
            android:drawableStart="@drawable/ic_action_cancel"
            android:focusable="true"
            android:text="Cancel"
            android:textColor="#78858B"
            android:textStyle="bold" />

        <Button
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/color_button"
            android:clickable="true"
            android:drawableStart="@drawable/ic_action_add_person"
            android:focusable="true"
            android:textColor="#78858B"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 我复制了你的示例 xml,但这是结果link
【解决方案3】:

**> > 以线性布局为父,滚动视图为子。

xmlns:android="http://schemas.android.com/apk/res/android" 机器人:方向=“垂直” android:layout_width="fill_parent" android:layout_height="fill_parent">

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/feedback_email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:ems="10"
    android:gravity="center"
    android:hint="Your Email Address"
    android:inputType="textEmailAddress"
    android:padding="5dp"
    android:textColor="#666"
    android:typeface="serif"
    android:background="@drawable/edittext_feedback"
     />

<Button
    android:id="@+id/feedback_submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/button_shape"
    android:padding="15dp"
    android:text="Submit Feedback"
    android:textColor="#666"
    android:typeface="monospace" /> </LinearLayout> </ScrollView>

**

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    相关资源
    最近更新 更多