如何利用碎片在一个屏幕上打开两个网页视图 [英] How to open two webviews in one screen using fragments
问题描述
可以在任何一个可以帮助我在搞清楚如何使用Android的碎片在同一屏幕中打开两个网页视图,每个网页视图必须显示特定网页,例如:1-谷歌,2,雅虎
我试过太多的教程和样本.nothing为我工作得很好...:(
对我来说,主要问题是冲突的我的想法,是在片段类写打开网页视图和什么运行整个应用程序的主要活动写了。
在此先感谢您的帮助..:)
这是我的code运行罚款有一个屏幕和崩溃景观模式人像模式:
包com.example.androidwebviewfragment;
进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;公共类片段1片段扩展{ 的WebView myWebView;
最终静态字符串myBlogAddr =http://android-er.blogspot.com;
串myUrl;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.layout_webfragment,集装箱,FALSE);
myWebView =(的WebView)view.findViewById(R.id.mywebview); myWebView.getSettings()setJavaScriptEnabled(真)。
myWebView.setWebViewClient(新MyWebViewClient()); 如果(myUrl == NULL){
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl); 返回视图。 } 私有类MyWebViewClient扩展WebViewClient {
@覆盖
公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
myUrl =网址;
view.loadUrl(URL);
返回true;
}
} @覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
setRetainInstance(真);
}}
和这里是第二个片段:
包com.example.androidwebviewfragment;进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;
公共类Fragment2扩展片段{ 的WebView myWebView;
最终静态字符串myBlogAddr =http://android-er.blogspot.com;
串myUrl;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看查看= inflater.inflate
(R.layout.layout_webfragment2,集装箱,FALSE);
myWebView =(的WebView)view.findViewById(R.id.mywebview); myWebView.getSettings()setJavaScriptEnabled(真)。
myWebView.setWebViewClient(新MyWebViewClient()); 如果(myUrl == NULL){
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl); 返回视图。 } 私有类MyWebViewClient扩展WebViewClient {
@覆盖
公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
myUrl =网址;
view.loadUrl(URL);
返回true;
}
} @覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
setRetainInstance(真);
}}
和这里的主要活动:
包com.example.androidwebviewfragment;
进口android.app.Activity;
进口android.os.Bundle;公共类MainActivity延伸活动{/ *(非Javadoc中)
* @see android.app.Activity#的onCreate(android.os.Bundle)
* /
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
}}.xml文件有:
1片段1:
<的WebView
机器人:ID =@ + ID / mywebview
机器人:layout_height =match_parent
机器人:layout_width =match_parent/>< / LinearLayout中>
2 - 片段2:
< / LinearLayout中>
3,主要XML布局:
<片段
机器人:名字=com.example.androidwebviewfragment.Fragment1
机器人:ID =@ + ID / myweb_fragment1
机器人:layout_height =match_parent
机器人:layout_width =match_parent/> <片段
机器人:名字=com.example.androidwebviewfragment.Fragment2
机器人:ID =@ + ID / myweb_fragment2
机器人:layout_height =match_parent
机器人:layout_width =match_parent/> < / RelativeLayout的>
这是布局土地文件夹中的main.xml:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向>
<片段
机器人:名字=com.example.androidwebviewfragment.Fragment1
机器人:ID =@ + ID / myweb_fragment
机器人:layout_height =match_parent
机器人:layout_width =match_parent/> <片段
机器人:名字=com.example.androidwebviewfragment.Fragment2
机器人:ID =@ + ID / myweb_fragment
机器人:layout_height =match_parent
机器人:layout_width =match_parent/> < / LinearLayout中>
从登录景观模式下的错误:
了java.lang.RuntimeException:
无法启动活动ComponentInfo {com.example.androidwebviewfragment
/com.example.androidwebviewfragment.MainActivity}
:android.view.InflateException:二进制XML文件行#12:错误充气类片段
在创建视图,并返回到主活动和创建绑定片段一fragmentadapter在MainActivity片段类。见( http://developer.android.com/reference/android /support/v4/view/ViewPager.html )链接查看更多细节,看到(的 http://www.vogella.com/articles/AndroidFragments/article.html )的例子。
could any one help me in figuring out how to open two webviews in the same screen using fragments Android,each webview must display a certain web page for example :1-google,2,yahoo.
i've tried too many tutorials and samples .nothing works fine for me... :(
The main issue for me that conflicting my thoughts, is what to write in the fragment class to open a webView and what to write in the main activity that runs the whole app .
Thanks in advance for any help.. :)
here is my code that runs fine for the portrait mode with one screen and crashes on landscape mode:
package com.example.androidwebviewfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Fragment1 extends Fragment {
WebView myWebView;
final static String myBlogAddr = "http://android-er.blogspot.com";
String myUrl;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_webfragment,container,false);
myWebView = (WebView)view.findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
if(myUrl == null){
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
and here is the second fragment :
package com.example.androidwebviewfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Fragment2 extends Fragment {
WebView myWebView;
final static String myBlogAddr = "http://android-er.blogspot.com";
String myUrl;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate
(R.layout.layout_webfragment2,container,false);
myWebView = (WebView)view.findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
if(myUrl == null){
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
And here is the main activity: package com.example.androidwebviewfragment;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the .xml files are :
1-Fragment 1 :
<WebView
android:id="@+id/mywebview"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
2-fragment 2 :
</LinearLayout>
3-main xml layout:
<fragment
android:name="com.example.androidwebviewfragment.Fragment1"
android:id="@+id/myweb_fragment1"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<fragment
android:name="com.example.androidwebviewfragment.Fragment2"
android:id="@+id/myweb_fragment2"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</RelativeLayout>
And this is the main.xml in the layout-land folder:
<?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="horizontal" >
<fragment
android:name="com.example.androidwebviewfragment.Fragment1"
android:id="@+id/myweb_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<fragment
android:name="com.example.androidwebviewfragment.Fragment2"
android:id="@+id/myweb_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
The error on land scape mode from log:
java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.androidwebviewfragment
/com.example.androidwebviewfragment.MainActivity}
:android.view.InflateException: Binary XML file line #12: Error inflating class fragment
In the fragment class you create view and returns it to main activity and in the mainactivity you create a fragmentadapter to bind fragments. See (http://developer.android.com/reference/android/support/v4/view/ViewPager.html) link for more details and see (http://www.vogella.com/articles/AndroidFragments/article.html) for examples.
这篇关于如何利用碎片在一个屏幕上打开两个网页视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!