【问题标题】:View.Frame equivalent in the AndroidAndroid 中的 View.Frame 等效项
【发布时间】:2015-11-17 23:10:34
【问题描述】:

我一直在研究iOS,并实现了以下代码,它在视图中显示WebView

UIView contentView = new UIView();
UIWebView webView = new UIWebView(contentView.Frame);

我想知道上面的代码如何写在Android中。我写了以下内容,但出现错误。我是安卓新手。

View contentView = new View();
WebView webView = new WebView(contentView.LayoutParamaters);

【问题讨论】:

    标签: android ios xamarin


    【解决方案1】:

    我认为这是对 iOS 和 Android 之间的差异以及 Xamarin 如何支持跨平台 C# 的误解。两个平台之间的 API 非常不同,而 UIView 在概念上类似于 View,实际上它们映射到不同的 API。 UIView 是 iOS 特定的 API,View 是 Android 特定的 API,它们都是相应原生控件之上的薄层,彼此无关。

    我建议通过一些 Xamarin.Android 教程来了解 Android 和 iOS 之间的差异。

    关于错误:

    View 的构造函数需要通过构造函数提供Context。您可以通过 2 种方式执行此操作,提供拥有的活动或全局应用程序上下文:

    // In the scope of an Activity.
    View contentView = new View(this); // "this" is the activity reference.
    
    // In another scope...
    View contentView = new View(Android.App.Application.Context);
    

    对于您的第二个错误,WebView 没有采用该参数的构造函数。

    什么都不提供来解决它:

    WebView webView = new WebView();
    

    【讨论】:

      【解决方案2】:

      这是一个如何显示 WebView 的最小示例。您的 Activity 的布局 XML 将是这样的:

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

      而你的 Activity 的代码就是这样......

      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.webkit.WebView;
      
      public class WebViewActivity extends AppCompatActivity {
      
          WebView webView;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.your_activity_layout);
      
              webView = (WebView) findViewById(R.id.yourWebView);
              webView.setWebViewClient(new WebViewClient());
              webView.loadUrl("http://www.google.com/");
          }
      }
      

      或者,如果您想以编程方式添加 WebView,您的 Activity 代码将如下所示...

      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.webkit.WebView;
      import android.webkit.WebViewClient;
      import android.widget.RelativeLayout;
      
      public class WebViewActivity extends AppCompatActivity {
      
          WebView webView;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_web_view);
      
              RelativeLayout parent = (RelativeLayout) findViewById(R.id.containerView);
              webView = new WebView(this);
              webView.setWebViewClient(new WebViewClient());
              webView.loadUrl("http://www.google.com/");
              parent.addView(webView, 0);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-07-17
        • 2012-08-03
        • 2011-10-29
        • 2011-09-07
        • 1970-01-01
        • 1970-01-01
        • 2010-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多