【问题标题】:Extending a custom layout class - Constructor never used扩展自定义布局类 - 从未使用过构造函数
【发布时间】:2015-09-11 18:05:19
【问题描述】:

我正在使用here给出的 GifWebView

我知道 Main Activity 等工作正常,因为我在用 XML 实现 Frame Layout 之前对其进行了测试。但是现在我使用的是Frame Layout,我遇到了问题。

我将向您提供我的代码和快速解释:

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);

    }
}

我想我的问题就在这里。我在网上看到我应该添加上面看到的后两个构造函数方法。但似乎第一个是唯一被使用的,即 - 后两个构造函数发出警告methods never used

也许我错了,但据说您需要这些构造函数来实现 XML 中的布局类,而这些构造函数从未被使用过……为什么?

以下两行在我的 MainActivity 中只是为了帮助您了解发生了什么。

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

非常感谢您提供的任何帮助,这个问题已经困扰我好几个小时了。

问题

Android 屏幕上什么都没有显示...只是空白

为黑带编辑

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());
    }
}    

【问题讨论】:

    标签: java android xml android-layout view


    【解决方案1】:

    您不应该重载视图的构造函数。使用

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

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

    并以编程方式提供非默认参数或为您的自定义View定义自定义属性

    【讨论】:

    • 感谢您的回答,您能否稍微扩展一下,假设我知道的很少,因为我只是一个初学者。 “非默认参数”或“我的自定义 View 的自定义属性”是什么意思?这会解决我的问题还是只是一个好习惯?应该使用那些构造方法吗?为什么不是我的?
    • 好的,我想我明白你的意思了。所以我在 values 文件夹中创建了一个attrs.xml 文件。然后我在其中创建一个新布局。我在我的主 xml 文件中使用这个布局,我可以添加样式和自定义属性。然后我从类中访问这些属性。我有 2 个问题。如何指定新布局对应 GifWebView?我是否保留所有 3 个构造函数并将 loadURL() 方法放入所有构造函数中。非常感谢您对我的耐心,如果这行得通,我会欣喜若狂
    • 哦,我想出了第一个问题,但不知道第二个问题。
    • 我是否保留所有 3 个构造函数并将 loadURL() 方法全部放入其中?
    • 谢谢,我似乎不知道用什么替换“myapp”。
    猜你喜欢
    • 1970-01-01
    • 2013-11-15
    • 2021-02-13
    • 2020-09-15
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多