你如何正确地写,在Android中使用View子类? [英] How do you properly write and use a View subclass in Android?

查看:142
本文介绍了你如何正确地写,在Android中使用View子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的解决方案之一找到<一href=\"http://stackoverflow.com/questions/4065134/is-there-a-listener-for-when-the-webview-displays-its-content/4235759#4235759\">here.

我的问题是,我不知道如果我实施和使用正确我的子类。
我在这里一个子类的WebView:

 公共类myWebView扩展的WebView {  公共myWebView(上下文的背景下){
    超级(上下文);
    // TODO自动生成构造函数存根
}  @覆盖
保护无效onSizeChanged(INT W,INT小时,INT流,诠释哦){
    // TODO自动生成方法存根      scrollTo(xScroll - (widthScroll / 2),yScroll - (heightScroll / 2));
    super.onSizeChanged(W,H,嗷嗷,哦);
}   }}

这应该是私有的,但忘了现在。我有我的活动,在视图层次结构内的WebView的一个内部的code。该活动的onCreate方法之外。

在onCreate方法,我有: myWebView mapImage =(myWebView)findViewById(R.id.mapcroppic);

这让我对这一呼吁一个ClassCastException。 (是否XML布局文件需要使用&LT; myWebView&GT;?可以的话)?我如何使用这个正确的方法


解决方案

  

    

(是否XML布局文件需要使用&LT; myWebView&GT;?可以的话)?我如何使用这个正确的方法


  

是:

 &LT; your.package.name.myWebView
    机器人:layout_with =嗒嗒/&GT;

好吧,这工作,如果在 myWebView 是一个公共类。如果它是一个内部之一:

 &LT;视图类=$ your.package.name.myWebView.YourActivity myWebView/&GT;

I am trying to implement one of the solutions found here.

My problem is that I'm not sure if I am implementing and using my subclass correctly. I am subclassing a WebView here:

public class myWebView extends WebView{

  public myWebView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

  @Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
    // TODO Auto-generated method stub

      scrollTo(xScroll - (widthScroll/2), yScroll - (heightScroll/2));


    super.onSizeChanged(w, h, ow, oh);
}

   }}

It should be private but forget that for now. I have the code inside of one of my activities that has an inner webview in the view hierarchy. Outside of the onCreate method of that activity.

Inside the onCreate method, I have:myWebView mapImage = (myWebView) findViewById(R.id.mapcroppic);

This gives me a ClassCastException for that call. (Does the xml layout file need to use <myWebView>? Can it?) How do I use this the correct way?

解决方案

(Does the xml layout file need to use <myWebView>? Can it?) How do I use this the correct way?

Yes:

<your.package.name.myWebView
    android:layout_with="blah"/>

Well, that works if the myWebView is a public class. If it's an inner one:

<view class="your.package.name.myWebView.YourActivity$myWebView" />

这篇关于你如何正确地写,在Android中使用View子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆