开发者

How to show a WebView with Theme.Dialog style in Android

I declared a WebView activity in the manifest like this:

<activity android:name=".MyWebView"
    android:label="@string/app_name"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@android:style/Theme.Dialog">
</activity>

The We开发者_如何转开发bView looks like this:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
    <WebView android:id="@+id/webview"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
    />
</LinearLayout>

When I start this activity within my main activity, only the title of the dialog, containing the apps name, is visible but not the WebView. If I add a TextView to the LinearLayout, it is shown as well, but still the WebView is missing. If I don't apply android:theme="@android:style/Theme.Dialog" in the manifest, the WebView is displayed.

Why is that and how can I show a WebView in a dialog?


I found that it works if you change the webview to wrap_content rather than fill_parent. No idea why. It's probably a bug.


The best way I have found is a little trick like this:

Notice the 1000dp for minWidth and minHeight

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:minWidth="1000dp"
          android:minHeight="1000dp" >

    <WebView android:id="@+id/web_view"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" />

</LinearLayout>

The other way is to wrap content on the WebView but it wont expand until you load a webpage..so it will start small, then expand. This hackish way will make the large dialog at start.


I would think what you are doing would work. Maybe try to override the dialog class instead of the activity class.


Use the WebView as the root layout - without any surrounding element. Don't know why it didn't work with the surrounding layout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜