[英]Extending a custom layout class - Constructor never used
I am using the GifWebView given here 我使用给出的GifWebView 这里
I know the Main Activity etc. is working fine, as I tested it before implementing in XML for Frame Layout. 我知道Main Activity等工作正常,因为在用XML实现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 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 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, ie - the second two constructors give warning
methods never used
. 但是似乎第一个是唯一使用的方法,即-后两个构造函数给出了
methods never used
警告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? 也许我是错的,但据推测您需要这些构造函数以XML形式实现布局类,而这些构造函数从未被使用过……为什么?
The following two lines are in my MainActivity just to help you see what's going on. 下面的两行在我的MainActivity中,只是为了帮助您了解发生了什么。
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 Android屏幕上没有任何显示...只是空白
EDIT FOR BLACKBELT 编辑黑带
activity_main.xml 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 attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GifWebView">
<attr name="fpath" format="string" />
</declare-styleable>
</resources>
GifWebView.java 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. 您不应该重载View的构造函数。 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
并以编程方式提供not-default-parameters或为您的自定义
View
定义自定义属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.