相关疑难解决方法(0)

Android自定义视图构造函数

我正在学习如何使用以下自定义视图:

http://developer.android.com/guide/topics/ui/custom-components.html#modifying

描述说:

类初始化与往常一样,首先调用super.此外,这不是默认构造函数,而是参数化构造函数.当EditText从XML布局文件中膨胀时,会使用这些参数创建EditText,因此,我们的构造函数需要同时接受它们并将它们传递给超类构造函数.

有更好的描述吗?我一直试图弄清楚构造函数应该是什么样子,我想出了4种可能的选择(参见帖子末尾的例子).我不确定这4个选择是做什么(或不做什么),为什么要实现它们,或者参数是什么意思.有这些的描述吗?

public MyCustomView()
{
    super();
}

public MyCustomView(Context context)
{
    super(context);
}

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

public MyCustomView(Context context, AttributeSet attrs, Map params)
{
    super(context, attrs, params);
} 
Run Code Online (Sandbox Code Playgroud)

xml android custom-view

34
推荐指数
3
解决办法
3万
查看次数

要为视图调用哪个构造函数?

我从我的自定义视图扩展View.有3个视图构造函数:

  1. View(Context context, AttributeSet attrs, int defStyle)
  2. View(Context context, AttributeSet attrs)
  3. View(Context context)

从我的活动我打电话std.setContentView(R.layout.main).第二个构造函数在我的视图中被调用.为什么第二个?如何预先知道哪一个将被调用以及为什么?

android android-layout

21
推荐指数
1
解决办法
6860
查看次数

自定义视图扩展相对布局

package com.binod.customviewtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

public class CustomView extends RelativeLayout{

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

//      LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutInflater mInflater = LayoutInflater.from(context);
        mInflater.inflate(R.layout.custom_view  , this, true);
    }

}
Run Code Online (Sandbox Code Playgroud)

包括为

<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"
    tools:context=".MainActivity" >

    <com.binod.customviewtest.CustomView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></com.binod.customviewtest.CustomView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

自定义视图为

<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"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

刚刚开始添加新的自定义视图并获得错误一次如果我清除它然后可以继续前进

我正在崩溃"引起:android.view.InflateException:二进制XML文件行#1:错误膨胀类"

android custom-view

20
推荐指数
1
解决办法
3万
查看次数

我应该调用super()还是为Android自定义视图构造函数调用this()?

在创建自定义视图时,我注意到许多人似乎这样做:

public MyView(Context context) {
  super(context);
  // this constructor used when programmatically creating view
  doAdditionalConstructorWork();
}

public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // this constructor used when creating view through XML
  doAdditionalConstructorWork();
}

private void doAdditionalConstructorWork() {
  // init variables etc.
}
Run Code Online (Sandbox Code Playgroud)

我的问题在于它阻止我使我的变量最终.有什么理由不做以下事情?

public MyView(Context context) {
  this(context, null);
  // this constructor used when programmatically creating view
}

public MyView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
  // this constructor used when creating view through XML
}

public …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view

12
推荐指数
3
解决办法
3923
查看次数

如何在Android-Scala应用程序中扩展ImageView?

我通过关键字在谷歌尝试了许多解决方案:多个构造函数,scala,继承,子类.

似乎没有人适合这个场合.ImageView有三个构造函数:

ImageView(context)
ImageView(context,attribute set)
ImageView(context,attribute set, style)
Run Code Online (Sandbox Code Playgroud)

在scala中,您只能扩展其中一个.使用更完整的构造函数(ImageView(context,attribute set, style))并传递默认值的解决方案也不起作用,因为构造函数ImageView(context)执行的操作与其他两个构造函数完全不同.

使用特征或伴随对象的一些解决方案似乎不起作用,因为CustomView必须是一个类!我的意思是我不是唯一一个使用这个类的人(所以我可以按照我想要的方式编写scala代码)还有使用这个类的android-sdk,是的,它必须是一个类.

target是一个扩展ImageView的CustomView,所有这些工作:

new CustomView(context)
new CustomView(context,attribute set)
new CustomView(context,attribute set, style)
Run Code Online (Sandbox Code Playgroud)

如果您需要进一步澄清这个棘手的问题,请告诉我!

inheritance android scala multiple-constructors

11
推荐指数
3
解决办法
1219
查看次数