如何在Fragment类中加载WebView?

时间:2017-11-16 06:06:43

标签: android android-fragments fragment findviewbyid

我正在尝试在片段中创建一个应该加载的WebView。我已将它添加到我的xml文件中。但是,findViewById方法仅在我扩展Activity类时才有效。无论如何我也可以在片段中使用它吗?

我已经从另一个问题测试了解决方案,但它没有用。

ffmpeg -i ./based_video/480/clip3.mp4 -filter_complex "color=black:100x100[c]; [c][0]scale2ref[ct][mv0]; \
[ct]setsar=1,split=3[t1][t2][t3]; \
[t1]drawtext=fontfile=/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf:text='\$30,000.0':fontsize=40:fontcolor=white,split[text1][alpha1]; \
[text1][alpha1]alphamerge,fade=t=in:st=1:d=1:alpha=1,fade=t=out:st=5:d=1:alpha=1[txta1]; \
[t2]drawtext=fontfile=/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf:text='\$30,000.0':fontsize=40:fontcolor=white,split[text2][alpha2]; \
[text2][alpha2]alphamerge,fade=t=in:st=1:d=1:alpha=1,fade=t=out:st=5:d=1:alpha=1[txta2]; \
[t3]drawtext=fontfile=/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf:text='\$30,000.0':fontsize=40:fontcolor=white,split[text3][alpha3]; \
[text3][alpha3]alphamerge,fade=t=in:st=1:d=1:alpha=1,fade=t=out:st=5:d=1:alpha=1[txta3]; \
[mv0][txta1]overlay=x='100':y='200':shortest=1[mv1]; \
[mv1][txta2]overlay=x='300':y='200':shortest=1[mv2]; \
[mv2][txta3]overlay=x='500':y='200':shortest=1" \
-c:v libx264 -c:a copy ./output_video/testnew-clip3-output.mp4

3 个答案:

答案 0 :(得分:1)

请试试这个,这是 OneFragment 类,Webview需要一个必须使用xml充气的视图

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout. fragment_one, container, false);
    mWebView = (WebView) view.findViewById(R.id.webview);
    mWebView.loadUrl("https://google.com");

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());

    return view;
}
fragment_one

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

答案 1 :(得分:0)

在Fragment中,您可以在onCreateView方法中声明变量。

public class OneFragment extends Fragment{

public WebView PlanWebView;

public OneFragment() {
// Required empty public constructor
}

@Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    }

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {


 }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
PlanWebView  = (WebView)view.findViewById(R.id.PlanWebView);
return view;
   }

 }

答案 2 :(得分:0)

您应该在onCreateView方法中使用以下代码:

View view = inflater.inflate(layout,container,false);

WebView webview = (WebView) view.findViewById(R.id.webView);

return view;