所以我正在尝试做这个教程http://code.tutsplus.com/tutorials/android-sdk-creating-custom-views--mobile-14548来创建一个自定义视图按钮,以便我可以在屏幕上绘制粉红色圆圈我在这里尝试做教程时出现错误是主档。
public class Main extends ActionBarActivity {
Draw v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout1 = new LinearLayout (this);
FrameLayout game = new FrameLayout(this);
Draw v = new Draw (this, null);
game.addView(v);
game.addView(layout1);
setContentView(game);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这里是XML文件,这是我得到错误的地方
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.Tripps.test.Draw"
xmlns:Tripps="http://schemas.android.com/apk/res/com.Tripps.test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.Tripps.test.Main" >
<com.Tripps.test.Draw
android:id="@+id/custView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
custom:circleColor="#ff0099"
custom:circleLabel="Hello"
custom:labelColor="#ffff66"
</com.Tripps.test.Draw>
</RelativeLayout>
这里是扩展viewc的Draw类,这是我想要引用的
public class Draw extends View {
//circle and text colors
private int circleCol, labelCol;
//label text
private String circleText;
//paint for drawing custom view
private Paint circlePaint;
public Draw(Context context, AttributeSet attrs){
super(context, attrs);
//paint object for drawing in onDraw
circlePaint = new Paint();
//get the attributes specified in attrs.xml using the name we included
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.DrawV, 0, 0);
}
@Override
protected void onDraw(Canvas canvas) {;
}
}
以下是教程告诉我在values文件夹中执行名为attr的文件。在我不知道为什么,但我把它放在那里
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Draw">
<attr name="circleColor" format="color" ></attr>
<attr name="circleLabel" format="string"></attr>
<attr name="labelColor" format="color"></attr>
</declare-styleable>
</resources>
答案 0 :(得分:1)
教程告诉我在values文件夹中创建一个名为attr的文件。在 我不知道为什么,但我把它放在那里
要在自定义视图中使用自定义属性,请在attrs.xml
文件中添加所有属性。
在attrs.xml
文件夹中创建res/values
文件。