Android layout 无法从Android中的XML布局膨胀自定义视图
我已经为它创建了一个CustomView和一个xml布局,以便能够为我的活动膨胀它,但是当运行活动时,我从未获得任何布局,而是收到一条错误消息,指示解析xml文件布局时出错 这是我的活动 包com.example.myprojectAndroid layout 无法从Android中的XML布局膨胀自定义视图,android-layout,Android Layout,我已经为它创建了一个CustomView和一个xml布局,以便能够为我的活动膨胀它,但是当运行活动时,我从未获得任何布局,而是收到一条错误消息,指示解析xml文件布局时出错 这是我的活动 包com.example.myproject import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity {
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_view_layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and this is my custom view
package com.example.myproject;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.GREEN);
}
}
and this is the layout i made for it
<com.exemple.myproject.CustomView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</com.exemple.myproject.CustomView>
and this is my Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom\u view\u layout);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}
这是我的自定义视图
包com.example.myproject;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.view.view;
公共类CustomView扩展了视图{
公共自定义视图(上下文){
超级(上下文);
}
受保护的void onDraw(画布){
画布。drawColor(颜色。绿色);
}
}
这是我为它做的布局
这是我的Android清单
可能是“示例”而不是“示例”
并添加
xmlns:yourapp=”http://schemas.android.com/apk/res-auto"
如果您有自定义的视图参数。我查看了Google,特别是stackoverflow,发现了一些类似的错误和解决方案,比如让我的活动扩展FragmentActivity而不是正常的活动,但这对我不起作用,并且在清单中建议了元数据标记,但我真的不知道怎么做要使用它,'example'没有错误,因为我在包名中没有写'example',请您清除属性xmlns:youapp好吗?顺便说一句,它对我不起作用。请注意,在你的代码片段中,“你的确切意思是什么?”?我应该特别避免什么样的错误?'xmlns:youapp…'应该有效,它使您能够为视图创建自定义参数,例如添加直径或半径:yourapp:diameter=“1.0”或yourapp:radius=“1.0”。
com.exemple.myproject.CustomView