安卓滚动视图太长

4

我对Android完全不熟悉 -

我在屏幕上放置了一个滚动视图,并在其中放置了一些文本。 我这样做是因为在某些手机上,文本会超出屏幕。

我的问题是,我放置在页面上的滚动视图比它所包含的内容更长 - 手机屏幕越窄,滚动视图就越长。

<?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="wrap_content"
    android:fillViewport="true" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/app_background"
    android:orientation="vertical"
    android:padding="10dp" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="20"
        android:inputType="textMultiLine"
        android:lineSpacingExtra="3dp"
        android:text="Get ready to melt fat and burn calories with Fit Body Boot Camp&apos;s 14 Day Fat Furnace Workout Program! This is a detailed 14 Day Workout plan based on Fit Body Boot Camp&apos;s Unstoppable Fitness Formula that has been implemented in close to 300 boot camps worldwide by hundreds of thousands of clients who made the decision to lose weight and get healthier."
        android:textColor="#FFF"
        android:textColorLink="#FFF"
        android:textSize="20sp" >
    </EditText>

这只是其中一部分,后面还有更多的EditText。如果您发现任何错误,请告诉我。


你能提供一张手机或模拟器上的截图吗? - Joseph Selvaraj
2个回答

11

您的背景在ScrollView中的线性布局中。如果将ScrollView放置在另一个线性布局中,该布局是ScrollView的父级并包含背景,并从线性布局中删除作为ScrollView子级的背景,则问题将得到解决。

它会看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="@drawable/app_background" >
<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="20"
    android:inputType="textMultiLine"
    android:lineSpacingExtra="3dp"
    android:text="THE LONG TEXT YOU'VE TYPED"
    android:textColor="#FFF"
    android:textColorLink="#FFF"
    android:textSize="20sp" >
</EditText>
</LinearLayout>
</ScrollView>
</LinearLayout>

1
我认为scrollview应该填充父容器,而子视图应该包裹内容,因为layout_height是视图在屏幕上实际拥有的大小,滚动发生在视图内部。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    //...
    android:layout_height="fill_parent"
    //...
>

注意:更好的使用“match_parent”而不是“fill_parent”,因为后者已被弃用。结果是相同的,只是名称已更改,只是因为“fill”很容易混淆,如果设置了它,它不会填充剩余的空间,而是根据其父元素设置尺寸。

1
最好使用“match_parent”代替。fill_parent“从API Level 8开始已被弃用,并被match_parent取代”(http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html) - dimi
@lelloman 我将 ScrollView 改为 match_parent,它的子级改为 wrap_content,但是没有任何变化。增加的长度似乎来自线性布局,因为当我删除它时,屏幕长度恢复正常。顺便说一句,我在 eclipse 中看到了增加的长度,不仅仅是在模拟器中。 - Adama
@lelloman,问题似乎是我有一个重复的背景资产,当它重复时,它会扩展屏幕很多。我将制作一个较小的平铺背景,以便它不会延伸太远。但是,难道没有办法使其不延伸屏幕吗? - Adama
这听起来很奇怪,LinearLayout 的超出尺寸应该由 ScrollView 管理,无论子视图有多大,ScrollView(使用 layout_height="match_parent")都不能比屏幕更大...即使您将 LinearLayout 的 layout_height 设置为 "10000px",ScrollView 仍然应该与父容器的高度匹配...您的背景设置在哪里?是设置在子 LinearLayout 上吗? - lelloman
是的,背景设置在线性布局上 - 我将其更改为滚动视图,并解决了问题。我正在使用线性布局的layout_width; 当它应该自动跨越屏幕宽度时,指定宽度似乎是多余的? - Adama
显示剩余2条评论

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