xml中的Android自定义组件不会出现

时间:2013-11-07 10:54:55

标签: android android-custom-view

我有这个自定义视图:

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

但它不起作用


更新

这是完整的代码:

MyActivity.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);
  }
}

CustomView.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");
  }

}

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

3 个答案:

答案 0 :(得分:2)

通常你这样声明。

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

您还需要使用ContextAttributeSet

覆盖构造函数
CustomView(Context context, AttributeSet attrs)

答案 1 :(得分:0)

你必须像这样警告你的观点:

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

答案 2 :(得分:0)

这个构造函数,你写的:

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

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