我自定义了一个继承View的控件。重载了OnDraw 画了点东西然后我想用lineairLayout或者relativelayout什么的把它和另外一些默认的比如Button,ImageView放在一起,
但是如果像下面这样的方法却无法得到想要的结果,好像2个控件都在同一个地方 而不是想要的Button在MyView的下面
到底是怎么回事呢? 就算用LineairLayout也是一样
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> <com.entrainement.AndroidAnimation.MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imgView"
/>
<Button
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonNext"
android:text="To camera test"
android:layout_below="@id/imgView"
/>
</RelativeLayout>
但是如果像下面这样的方法却无法得到想要的结果,好像2个控件都在同一个地方 而不是想要的Button在MyView的下面
到底是怎么回事呢? 就算用LineairLayout也是一样
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> <com.entrainement.AndroidAnimation.MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imgView"
/>
<Button
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonNext"
android:text="To camera test"
android:layout_below="@id/imgView"
/>
</RelativeLayout>
解决方案 »
- 关于 android 不同进程间模拟按键和触摸事件
- Android平台下如何使用SSL访问服务器
- android动态墙纸问题
- 我这个空指针异常是在哪里出现的?
- 调用android输入法服务,获取字符问题?
- 关于Actionbar的一个问题
- 有没有做android4.4.2 settings的,求交流呀!
- AsyncHttpClient 上传两个以上文件出错 急!
- android 叉叉助手 弹幕原理
- Android Studio3.2.1在Build菜单没有Generate Signed Apk选项怎么回事
- 关于安装Android遇到的问题?
- 请问下android平台有没有类似3DMark06的程序apk?
public class MyView extends View{
Bitmap bitmap;
Paint paint;
public MyView(Context context) {
super(context);
paint = new Paint();
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context,attrs);
paint = new Paint();
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Style.FILL_AND_STROKE);
paint.setTextSize(15);
canvas.drawBitmap(bitmap,10,10,paint);
}
}
如果和其他控件一起放在Layout里无法像普通的预定义的控件一样能按着顺序自己安排
查了一下自定义的View还要重写 onMeasure计算View的合适高度和宽度,加上看一下 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(bitmap.getWidth(), bitmap.getHeight());
}