代码如下
为了更好的阅读体验,可以到我的博客进行阅读
https://qarks.top/
注释可能有英文语法问题,别在意,能看就行。
```java
package com.kands.qarks;
import android.app.*;
import android.os.*;
import android.util.*;
import android.widget.*;
import android.graphics.*;
import android.view.*;
import android.content.*;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// create a (frame)layout
FrameLayout frameLayout = new FrameLayout(this);
// set layout background
frameLayout.setBackgroundResource(R.mipmap.bg);
// add the layout to the application
setContentView(frameLayout);
// constructor parameter is a context object(instance)
TextView text1 = new TextView(this);
// set text, size (unit and scale)
text1.setText("Start");
text1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
// set font color
text1.setTextColor(Color.rgb(17, 85, 114));
// create LayoutParams instance and set size of layout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// set gravity of layout
params.gravity = Gravity.CENTER;
// make TextView text1 be central
text1.setLayoutParams(params);
// set a case of click (Listener)
text1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new AlertDialog.Builder(MainActivity.this).setTitle("Warning").setMessage("The picture is too beautiful").setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Log.i("kands", "See");
}
}).setNegativeButton("No", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Log.i("kands", "Bye");
// end the activity
finish();
}
}).show();
}
});
frameLayout.addView(text1);
}
}
```
不得不说用Java来写还真就比xml麻烦,可这也是一种方法,我们需要了解以及掌握部分方法。一起加油吧!