2014-01-30 33 views
0

我有一个布局,带有一些按钮在底部的文本视图。 textview设置为展开以填充空间。这可以。但是,当我将textview更改为webview时,webview不会扩展以填充空间。怎么了?TextView扩展布局,但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> 

这不是,但唯一不变的是对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

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

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

为了达到这个效果,我看到一些Android开发人员试图将scroll view中的视图高度设置为fill_parent。这样做不工作,导致以下结果:

FillScrollView example by Romain Guy

为了理解这个结果,你必须记住,机器人:layout_height =” FILL_PARENT”的意思是‘高度设置为父的高度。’这显然不是你使用ScrollView时想要的。毕竟,如果ScrollView的内容总是与自身一样高,它将变得毫无用处。

通过设置android:fillViewport="true",滚动视图的子级展开到ScrollView的高度。 (当孩子比ScrollView高,属性没有效果。)

+0

谢谢,抱歉,我忘了+1这个。我想我假设,因为我需要在textview周围使用滚动条布局,我必须对webview执行相同的操作。另外使用fillviewport意味着我可以删除有关使用fill_parent的警告并使用wrap_content :) –