MVC: Mode-View-Controller 是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。
1.在代码中动态生成布局:
LinearLayout linearLayout = new LinearLayout(this);
super.setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
show = new TextView(this);
Button button = new Button(this);
button.setText(R.string.ok);
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.addView(show);
linearLayout.addView(button);
2. ImageView显示图片:
imageView.setImageResource(R.drawable.img);
3.开发自定义View:
DrawView.java文件
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class DrawView extends View {
private float currentX = 40f;
private float currentY = 50f;
//定义并创建画笔
private Paint P = new Paint();
public DrawView(Context context) {
super(context);
}
public DrawView(Context context, AttributeSet set) {
super(context,set);
}
//初始化时,小圆点的位置
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
P.setColor(Color.RED);
canvas.drawCircle(currentX,currentY,15f,P);
}
//触碰时随手指移动
@Override
public boolean onTouchEvent(MotionEvent event) {
currentX = event.getX();
currentY = event.getY();
//刷新View
invalidate();
return true;
}
}
layout文件里应用DrawView:
<com.luo.codeview.DrawView
android:layout_width="match_parent"
android:layout_height="match_parent" />