TextView扩展了布局,但webview没有

时间:2014-01-30 08:44:28

标签: android-layout

我有一个带有textview的布局,底部有一些按钮。 textview设置为展开以填充空间。这可以。但是,当我将textview更改为webview时,webview不会展开以填充空间。怎么了?

这很好用:

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
    <!-- height set to fill because background is black and we want 
        the light background -->
    <TextView
        style="@style/subtopicView"
        android:id="@+id/subtopic_content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         />
    </ScrollView>

    <include layout="@layout/include_html_view_footer" />

</LinearLayout>

这不是,但唯一的变化是webview的textview和所有其他值保持不变。

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
    <!-- height set to fill because background is black and we want 
        the light background -->
         <WebView
             style="@style/subtopicView"
        android:id="@+id/subtopic_content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    </ScrollView>

    <include layout="@layout/include_html_view_footer" />

</LinearLayout>

包含就是这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/tintedColour"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/spacer"
    android:paddingTop="@dimen/spacer" >

    <Button
        android:id="@+id/PrevPage"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/previous" />

    <Button
        android:id="@+id/NextPage"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/next" />

    <Button
        android:id="@+id/Copy"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/copyclipboard" />

</LinearLayout>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

通过在ScrollView中包含WebView来嵌套可滚动元素通常是一个坏主意。触摸事件将是不可预测的,完全放弃ScrollView可能是一个更好的主意。

也就是说,WebView的高度设置为fill_parent。但是,ScrollView同时尝试最小化其子项的大小。 As Romain Guy writes in a blog post on this topic

  

为了达到这个效果,我看到几个Android开发人员试图将滚动视图中的视图高度设置为fill_parent。这样做不起作用,并导致以下结果:

     

FillScrollView example by Romain Guy

     

要理解这个结果,你必须记住android:layout_height =“fill_parent”的意思是“将高度设置为父级的高度。”这显然不是你在使用ScrollView时想要的。毕竟,如果ScrollView的内容总是和它本身一样高,那么它将变得毫无用处。

通过设置android:fillViewport="true",滚动视图的子项扩展到ScrollView的高度。 (当孩子比ScrollView高时,该属性无效。)

相关问题