編輯:關於Android編程
Android的彈窗效果有很多種,就最簡單而言,就可以調用一個AlertDialog彈窗顯示,可是要自定義彈窗效果有以下這種方法,就我個人而言感覺挺方便的,適用性也挺廣的。
首先先簡單寫個AlertDialog的使用
public void showDialog(){ AlertDialog dialog = new AlertDialog.Builder(this) .setTitle(提示) .setMessage(getResources().getString(你確定退出嗎?) .setPositiveButton(取消, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setNegativeButton(確定, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).create(); dialog.show(); } }
接下來就是自定義彈窗效果圖:
下面是示例代碼:
public class SelectPopupWindow extends PopupWindow { Button takePhoto; Button selectFromAlbum; Button cancel; View menuView; public SelectPopupWindow(Context ctx, OnClickListener onClickListener) { LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); menuView = inflater.inflate(R.layout.modify_head, null); //可以采用各種樣式,自定義view takePhoto = (Button) menuView.findViewById(R.id.takephoto); takePhoto.setOnClickListener(onClickListener); selectFromAlbum = (Button) menuView.findViewById(R.id.selectfromalbum); selectFromAlbum.setOnClickListener(onClickListener); cancel = (Button) menuView.findViewById(R.id.cancel); cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dismiss(); } }); setContentView(menuView); setWidth(LayoutParams.MATCH_PARENT); setHeight(LayoutParams.WRAP_CONTENT); setFocusable(true); //彈出窗體後可點擊 //setAnimationStyle(R.style.AnimBottom); //設置動畫 menuView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int height = menuView.findViewById(R.id.top).getTop(); Log.e(huang, top height:+height+ y:+event.getY()); int y = (int) event.getY(); if(y
public class TestPopupWindow extends Activity { private SelectPopupWindow popupWindow; private ImageView image; private Button btn, btn1; private Uri fileUri; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1); init(); } private void init(){ image = (ImageView) findViewById(R.id.image); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(onClickListener); btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(onClickListener); } View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn: test(); break; case R.id.btn1: startActivity(new Intent(TestPopupWindow.this, TestGsonActivity.class)); break; case R.id.takephoto: Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); startActivityForResult(intent, 100); break; case R.id.selectfromalbum: Intent intent1 = new Intent(Intent.ACTION_PICK); intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image/*); startActivityForResult(intent1, 200); break; } } }; public void test(){ popupWindow = new SelectPopupWindow(this, onClickListener); popupWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); }
一、實現效果: 最近在項目中需要做類似於上圖顯示的效果,裡面的數字和稱謂是動態獲取的,對於這種顯示效果,有如下兩種解決方案來處理: (1)通過代碼動態設置TextVie
起因: 最近在做一個新聞APP,看到現在的新聞客戶端頂端都有個熱點新聞輪播。思路:viewpager可以用來顯示圖片,並且可以提供滑動,15年(不知記錯沒)新出的TabL
一、調用Android lint命令查找出沒有用到的資源,並生成一個清單列表:命令:lint –check “UnusedResources” [project_path
先看一下效果:效果就是這樣,看一下實現,其實也沒多難,就是想開源出來供小伙伴們使用,如有不合理地方,希望大家多多指正。1.自定義PopWindow首先我們分析一下,這樣的