Toast:
1
| Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_SHORT).show();
|
1 2 3 4
| Toast toast=Toast.makeText(this, "居中Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0); toast.show();
|
- 自定义样式(布局文件根布局设置大小不管用)
布局文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" > <ImageView android:src="@drawable/ic_launcher_background" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" /> </LinearLayout> </LinearLayout>
|
java 中:
1 2 3 4 5 6 7 8 9 10
| LayoutInflater li=LayoutInflater.from(TestActivity.this); View view=li.inflate(R.layout.toast,null);
TextView textView=view.findViewById(R.id.text); textView.setText("自定义Toast"); Toast toast=new Toast(this); toast.setView(view); toast.setDuration(Toast.LENGTH_SHORT); toast.show();
|
AlertDialog:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setIcon(R.drawable.ic_launcher_background) .setTitle("标题").setMessage("内容") .setPositiveButton("确定按钮", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity.this, "确定按钮被按下", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消按钮", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity.this, "取消按钮被按下", Toast.LENGTH_SHORT).show(); } }) .setNeutralButton("其它", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity.this, "其它按钮被按下", Toast.LENGTH_SHORT).show(); } }); dialog.create();
AlertDialog ad=dialog.show(); ad.dismiss();
|
1 2 3 4 5 6 7 8 9 10
| final String[] texts=new String[]{"男","女"}; AlertDialog.Builder dialog2=new AlertDialog.Builder(this);
dialog2.setTitle("请选择").setItems(texts, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity.this, texts[which], Toast.LENGTH_SHORT).show(); } }) .create().show();
|
1 2 3 4 5 6 7 8 9 10 11 12
| final String[] texts2=new String[]{"男","女"}; AlertDialog.Builder dialog4=new AlertDialog.Builder(this);
dialog4.setTitle("请选择").setSingleChoiceItems(texts2, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity.this, texts2[which], Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }).setCancelable(false); dialog4.create().show(); break;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| final String[] texts3=new String[]{"一","二","三"}; final boolean[] checkde=new boolean[]{true,false,true}; AlertDialog.Builder dialog3=new AlertDialog.Builder(this);
dialog3.setTitle("请选择").setMultiChoiceItems(texts3, checkde, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(TestActivity.this, texts3[which]+":"+isChecked, Toast.LENGTH_SHORT).show(); } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .create().show();
|
1 2 3 4 5 6 7 8 9 10 11
| LayoutInflater li=LayoutInflater.from(TestActivity.this); View view=li.inflate(R.layout.toast,null);
TextView textView=view.findViewById(R.id.text); textView.setText("自定义Toast");
AlertDialog.Builder dialog3=new AlertDialog.Builder(this);
dialog3.setTitle("自定义") .setView(view).create().show();
|
ProgressBar:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <ProgressBar android:id="@+id/bar1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/bar2" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.Material.ProgressBar.Horizontal" android:max="100" android:progress="30" />
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| bar2=findViewById(R.id.bar2); System.out.println(bar2.getProgress());
handler.sendEmptyMessage(0); -----------------------------------------
Handler handler=new Handler(){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); if (bar2.getProgress()<100){ this.postDelayed(runnable,500); }else{ Toast.makeText(TestActivity.this, "完成", Toast.LENGTH_SHORT).show(); } } }; Runnable runnable=new Runnable() { @Override public void run() { bar2.setProgress(bar2.getProgress()+3); handler.sendEmptyMessage(0); } };
|
自定义一个弹出菜单:
1 2 3 4 5 6 7 8 9 10
| View view= LayoutInflater.from(FriendActivity.this).inflate(R.layout.fun_popup,null);
PopupWindow pop=new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(new BitmapDrawable()); pop.setFocusable(true); pop.setOutsideTouchable(true);
pop.showAsDropDown(item,0,-30);
|
AndroidStudio 使用 lombok:
在build.gradle中添加如下配置:
1 2 3 4
| dependencies { annotationProcessor 'org.projectlombok:lombok:1.18.12' compileOnly "org.projectlombok:lombok:1.18.12" }
|
解决弹出键盘导致布局改变:
在 onCreate 中的 setContentView 之前添加如下代码:
1
| getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
|