【问题标题】:Android Scroll View is Too LongAndroid 滚动视图太长
【发布时间】:2013-04-02 22:06:25
【问题描述】:

我是 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>

在这个之后还有一些编辑文本,但这是它的要点。如果您看到一些愚蠢的东西,请告诉我。

【问题讨论】:

  • 你能截图看看它在你的手机或模拟器中的样子吗?

标签: android xml eclipse android-scrollview


【解决方案1】:

您的背景位于 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>

【讨论】:

    【解决方案2】:

    我认为滚动视图应该填充父视图,子视图包裹内容,因为 layout_height 是视图在屏幕上实际具有的大小,滚动发生在视图内部

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        //...
        android:layout_height="fill_parent"
        //...
    >
    

    编辑

    正如 dbalaouras 所指出的,最好使用“match_parent”而不是“fill_parent”,因为后者已被弃用。结果是一样的,只是名字改了,只是因为“fill”比较混乱,如果你设置它不会填充剩余的空间而只是根据它的父级设置维度

    【讨论】:

    • 最好改用“match_parent”。 fill_parent "从 API 级别 8 开始弃用,由 match_parent 取代" (developer.android.com/reference/android/view/…)
    • @lelloman 我将滚动视图更改为 match_parent 及其子项以包装内容,但它没有改变任何内容。增加的长度似乎来自线性布局,当我删除它时,屏幕长度恢复正常。顺便说一句,我在 Eclipse 中看到了增加的长度,而不仅仅是在模拟器上。
    • @lelloman 问题似乎是我有一个重复的背景资产,当它重复时,它扩展了很多屏幕。我将制作一个较小的平铺背景,这样它就不会延伸太远。不应该有办法让它不扩展屏幕吗?
    • 这听起来很奇怪,linearlayout的超出尺寸应该由scrollview管理,不管子view有多大,scrollview(带有layout_height="match_parent")不能比屏幕大...即使您将 layout_height="10000px" 设置为线性布局,滚动视图仍应与父级的高度匹配...您的背景设置在哪里?给子线性布局?
    • @user2051879 顺便说一句,您是否使用 layout_WIDTH = "match_parent" 进行线性布局?
    猜你喜欢
    • 1970-01-01
    • 2018-03-12
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多