0

我有这个自定义视图:

package com.myapp;

public class CustomView extends View {

  public CustomView(Context c) {
    super(c);
    CharSequence text = "HELLO";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(c, text, duration);
    toast.show();
  }
}

我想通过xml放置它,所以这就是我写的main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/tableLayout1"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >
  <view
    class="com.myapp.CustomView"
    id="@+id/customText"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="@color/red"
  />
</LinearLayout>

内容strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#FF0000</color>
</resources>

无论如何,打开应用程序时我看不到红色视图,也看不到 Toast,我错过了什么?

更新:

忘记说了,我也试过这样:

<com.myapp.CustomView
  id="@+id/inputText"
  android:layout_width="match_parent"
  android:layout_height="30dp"
  android:background="@color/red"
/>

但它不起作用


更新

这是完整的代码:

我的活动.java

public class MyActivity extends Activity {
  /**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

自定义视图.java:

public class CustomView extends View {

  @Override
  public CustomView(Context context) {
    super(context);
    Log.d("myapp.contextview","context constructor");
    throw new RuntimeException("Trying to crash the app");
  }

  @Override
  public CustomView(Context context, AttributeSet attrs) {
    super(context,attrs);
    Log.d("myapp.contextview","context+attributeset constructor");
    throw new RuntimeException("Trying to crash the app");
  }

  @Override
  public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context,attrs,defStyleAttr);
    Log.d("myapp.contextview","context+attributeset+defstyleattr constructor");
    throw new RuntimeException("Trying to crash the app");
  }

}

主.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/tableLayout1"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >
  <com.myapp.CustomView
    id="@+id/customText"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="@color/red"
  />
</LinearLayout>

字符串.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#FF0000</color>
</resources>
4

3 回答 3

2

通常你是这样声明的。

 <com.myapp.CustomView     
  id="@+id/customText"
  android:layout_width="match_parent"
  android:layout_height="30dp"
  android:background="@color/red"
/>

您还需要使用Contextand覆盖构造函数AttributeSet

CustomView(Context context, AttributeSet attrs)
于 2013-11-07T10:57:36.830 回答
0

您必须像这样提醒您的视图:

<com.myapp.CustomView 
    android:id="@+id/customText"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="@color/red"
  />
于 2013-11-07T10:56:39.610 回答
0

这个构造函数,你写的:

public CustomView(Context c) {
    super(c);

call 并不总是,覆盖其他构造函数并将您的逻辑放入新方法init()

于 2013-11-07T10:59:38.370 回答