扩展的自定义布局类 - 构造从来没有使用过 [英] Extending a custom layout class - Constructor never used

查看:177
本文介绍了扩展的自定义布局类 - 构造从来没有使用过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用给出的GifWebView <一个href=\"http://stackoverflow.com/questions/6533942/adding-gif-image-in-an-imageview-in-android\">here

我知道,主要活动等工作正常,因为我在XML实施框架布局之前,测试它。但现在,我使用的框架布局,我遇到的问题。

我只是为你提供我的code和快速的解释:

XML

 &LT;的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT&GT;    &LT; com.example.dencallanan.giftestapp.GifWebView
        机器人:ID =@ + ID / GWebView
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent        /&GT;    &LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s的android:layout_width =match_parent
        机器人:layout_height =match_parent机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
        机器人:paddingTop =@扪/ activity_vertical_margin
        机器人:paddingBottom会=@扪/ activity_vertical_margin工具:上下文=MainActivity。&GT;        &LT;的EditText
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID / EDITTEXT
            机器人:layout_marginBottom =153dp
            机器人:layout_alignParentBottom =真
            机器人:layout_alignParentRight =真
            机器人:layout_alignParentEnd =真
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentStart =真
            机器人:提示=USERNAME/&GT;        &LT;的EditText
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID / editText2
            机器人:layout_marginTop =58dp
            机器人:layout_alignTop =@ + ID / EDITTEXT
            机器人:layout_alignParentRight =真
            机器人:layout_alignParentEnd =真
            机器人:layout_alignLeft =@ + ID / EDITTEXT
            机器人:layout_alignStart =@ + ID / EDITTEXT
            机器人:提示=密码/&GT;        &LT;按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=登录
            机器人:ID =@ + ID /按钮
            机器人:layout_below =@ + ID / editText2
            机器人:layout_centerHorizo​​ntal =真
            机器人:layout_marginTop =25dp/&GT;
    &LT; / RelativeLayout的&GT;
&LT; /&的FrameLayout GT;

GifWebView

 公共类GifWebView扩展的WebView {    公共GifWebView(上下文的背景下,字符串路径){        超级(上下文);
        使用loadURL(路径);
    }    公共GifWebView(上下文的背景下,ATTRS的AttributeSet,字符串路径){        超(背景下,ATTRS);    }    公共GifWebView(上下文的背景下,ATTRS的AttributeSet,INT defStyle,字符串路径){        超(背景下,ATTRS,defStyle);    }
}

我觉得在这里我的问题奠定。我看到网上说我要补充上面看到第二两个构造方法。但似乎第一个是正在使用的只有一个,即 - 第二两个构造给予警告方法从未使用过

也许我错了,但据说你需要这些构造函数来实现XML布局类,从不使用这些构造......为什么?

下面两行是我MainActivity只是为了帮助你看到发生了什么事情。

  gWView =(GifWebView)findViewById(R.id.GWebView);
    gWView =新GifWebView(这一点,文件:///android_asset/watg.gif);

我真的AP preciate任何帮助给定的,这个问题一直在我几个小时。

问题

没有显示Android的屏幕上......只是空白

编辑黑带

activity_main.xml中

 &LT;的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:的myapp =htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT&GT;    &LT; com.example.dencallanan.giftestapp.GifWebView
        机器人:ID =@ + ID / GWebView
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        的myapp:fpath =WATG&LT; - 还试图watg.gif - &GT;        /&GT;    &LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s的android:layout_width =match_parent
        机器人:layout_height =match_parent机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
        机器人:paddingTop =@扪/ activity_vertical_margin
        机器人:paddingBottom会=@扪/ activity_vertical_margin工具:上下文=MainActivity。&GT;        &LT;的EditText
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID / EDITTEXT
            机器人:layout_marginBottom =153dp
            机器人:layout_alignParentBottom =真
            机器人:layout_alignParentRight =真
            机器人:layout_alignParentEnd =真
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentStart =真
            机器人:提示=USERNAME/&GT;        &LT;的EditText
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID / editText2
            机器人:layout_marginTop =58dp
            机器人:layout_alignTop =@ + ID / EDITTEXT
            机器人:layout_alignParentRight =真
            机器人:layout_alignParentEnd =真
            机器人:layout_alignLeft =@ + ID / EDITTEXT
            机器人:layout_alignStart =@ + ID / EDITTEXT
            机器人:提示=密码/&GT;        &LT;按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=登录
            机器人:ID =@ + ID /按钮
            机器人:layout_below =@ + ID / editText2
            机器人:layout_centerHorizo​​ntal =真
            机器人:layout_marginTop =25dp/&GT;
    &LT; / RelativeLayout的&GT;
&LT; /&的FrameLayout GT;

attrs.xml

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
    &LT;申报-设置样式名称=GifWebView&GT;
        &LT; attr指示NAME =fpath格式=字符串/&GT;
    &LT; /申报,设置样式&GT;
&LT; /资源&GT;

GifWebView.java

 公共类GifWebView扩展的WebView {    公共GifWebView(上下文的背景下){        超级(上下文);
    }    公共GifWebView(上下文的背景下,ATTRS的AttributeSet){        超(背景下,ATTRS);        TypedArray A = context.obtainStyledAttributes(ATTRS,
                R.styleable.GifWebView);
        CharSequence中= a.getString(R.styleable.GifWebView_fpath);
        使用loadURL(s.toString());
    }    公共GifWebView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){        超(背景下,ATTRS,defStyle);        TypedArray A = context.obtainStyledAttributes(ATTRS,
                R.styleable.GifWebView);
        CharSequence中= a.getString(R.styleable.GifWebView_fpath);
        使用loadURL(s.toString());
    }
}


解决方案

你不应该超载查看的构造。使用

 公共GifWebView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
 }

 公共GifWebView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
}

和提供的默认参数,编程或定义自定义属性为您定制查看

I am using the GifWebView given here

I know the Main Activity etc. is working fine, as I tested it before implementing in XML for Frame Layout. But now that I am using Frame Layout, I am encountering problems.

I will just provide you with my code and a quick explanation:

XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.example.dencallanan.giftestapp.GifWebView
        android:id="@+id/GWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />

    <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" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_marginBottom="153dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:hint="USERNAME" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_marginTop="58dp"
            android:layout_alignTop="@+id/editText"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignLeft="@+id/editText"
            android:layout_alignStart="@+id/editText"
            android:hint="PASSWORD" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Log In"
            android:id="@+id/button"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp" />
    </RelativeLayout>


</FrameLayout>

GifWebView

public class GifWebView extends WebView {

    public GifWebView(Context context, String path) {

        super(context);
        loadUrl(path);
    }

    public GifWebView(Context context, AttributeSet attrs, String path) {

        super(context, attrs); 

    }

    public GifWebView(Context context, AttributeSet attrs, int defStyle, String path) {

        super(context, attrs, defStyle);

    }
}

I think my problem lays here. I saw online that I should add the second two constructor methods seen above. But it seems the first one is the only one being used, i.e - the second two constructors give warning methods never used.

Maybe I'm wrong but supposedly you need these constructors to implement the layout class in XML, and these constructors are never being used... why?

The following two lines are in my MainActivity just to help you see what's going on.

    gWView = (GifWebView) findViewById(R.id.GWebView);
    gWView = new GifWebView(this, "file:///android_asset/watg.gif");

I'd really appreciate any help given, this problem has been at me for hours.

Problem

Nothing shows on Android screen... just blank

EDIT FOR BLACKBELT

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.example.dencallanan.giftestapp.GifWebView
        android:id="@+id/GWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        myapp:fpath="watg"  <!-- also tried "watg.gif" -->

        />

    <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" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_marginBottom="153dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:hint="USERNAME" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_marginTop="58dp"
            android:layout_alignTop="@+id/editText"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignLeft="@+id/editText"
            android:layout_alignStart="@+id/editText"
            android:hint="PASSWORD" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Log In"
            android:id="@+id/button"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp" />
    </RelativeLayout>


</FrameLayout>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="GifWebView">
        <attr name="fpath" format="string" />
    </declare-styleable>
</resources>

GifWebView.java

public class GifWebView extends WebView {

    public GifWebView(Context context) {

        super(context);
    }

    public GifWebView(Context context, AttributeSet attrs) {

        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.GifWebView);
        CharSequence s = a.getString(R.styleable.GifWebView_fpath);
        loadUrl(s.toString());
    }

    public GifWebView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.GifWebView);
        CharSequence s = a.getString(R.styleable.GifWebView_fpath);
        loadUrl(s.toString());
    }
}    

解决方案

you shouldn't overload the View's constructor. Use

 public GifWebView(Context context, AttributeSet attrs) {
        super(context, attrs); 
 }

and

public GifWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
}

and provide not-default-parameters, programmatically or define a custom attribute for your custom View

这篇关于扩展的自定义布局类 - 构造从来没有使用过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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