Android开发中控制UI界面的方式
1.使用XML布局文件控制UI界面(推荐)

2.在Java代码中控制UI界面

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
frameLayout.setBackgroundResource(R.mipmap.bg);
setContentView(frameLayout);
TextView textView=new TextView(this);
textView.setText("开始游戏");
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
textView.setTextColor(Color.rgb(17,85,114));
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity= Gravity.CENTER;
textView.setLayoutParams(params);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
.setMessage("游戏有风险,进入需谨慎,真的要进入吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("桌面台球","进入游戏");
}
}).setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("桌面台球","退出游戏");
finish();//结束当前的activity
}
}).show();
}
});
frameLayout.addView(textView);
}
}
3.使用XML和Java代码混合控制UI界面

public class MainActivity extends AppCompatActivity {
private ImageView[] img=new ImageView[12];
private int[] imagePath=new int[]{
R.mipmap.img1,R.mipmap.img2,R.mipmap.img3,R.mipmap.img4,
R.mipmap.img5,R.mipmap.img6,R.mipmap.img7,R.mipmap.img8,
R.mipmap.img9,R.mipmap.img10,R.mipmap.img11,R.mipmap.img12,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout gridLayout=findViewById(R.id.layout);
for(int i=0;i<imagePath.length;i++){
img[i]=new ImageView(MainActivity.this);
img[i].setImageResource(imagePath[i]);
img[i].setPadding(2,2,2,2);
ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(130,80);
img[i].setLayoutParams(params);
gridLayout.addView(img[i]);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
android:orientation="horizontal"
android:rowCount="3"
android:columnCount="4"
tools:context=".MainActivity">
</GridLayout>
4.开发自定义的View

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/img1"
android:id="@+id/mylayout"
tools:context=".MainActivity">
</FrameLayout>
public class RabbitView extends View {
public float bitmapx,bitmapy;
public RabbitView(Context context) {
super(context);
bitmapx=290;
bitmapy=130;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint=new Paint();
Bitmap bitmap= BitmapFactory.decodeResource(this.getResources(),R.mipmap.rabbit);
canvas.drawBitmap(bitmap,bitmapx,bitmapy,paint);
if(bitmap.isRecycled()){
bitmap.recycle();
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout frameLayout=findViewById(R.id.mylayout);
final RabbitView rabbitView=new RabbitView(this);
rabbitView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
rabbitView.bitmapx=event.getX();
rabbitView.bitmapy=event.getY();
rabbitView.invalidate();
return false;
}
});
frameLayout.addView(rabbitView);
}
}
1120



被折叠的 条评论
为什么被折叠?



