Android UI平常

1. 使用java代碼自定義彈出框的樣式

public static void show(Context context, String title, String tips){

		   final ImageView aicon = new ImageView(context);
		   aicon.setId(android.R.id.icon);
		   aicon.setImageResource(android.R.drawable.ic_menu_close_clear_cancel);
		   LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		   LinearLayout linearLayout_780 = new LinearLayout(context);
			linearLayout_780.setOrientation(LinearLayout.HORIZONTAL);
			LayoutParams layout_746 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		 	linearLayout_780.setLayoutParams(layout_746);

			aicon.setLayoutParams(layoutParams);

			linearLayout_780.addView(aicon);
			linearLayout_780.setBackgroundResource(R.color.app_green);

			 
 		   builder.setCustomTitle(linearLayout_780);
		   final AlertDialog   localMyDialog   = builder.create();

		  localMyDialog.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		    localMyDialog.setButton(DialogInterface.BUTTON_POSITIVE ,tips, new NDialog(context)) ;
		    localMyDialog.setTitle(title);
//		    localMyDialog.setMessage(tips);

		    
		    localMyDialog.setCancelable(false);
		    localMyDialog.setCanceledOnTouchOutside(false);
//		    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);  
		    
		    localMyDialog.getWindow().setType(2003);
		    localMyDialog.show();

		    TextView textView = (TextView) localMyDialog.findViewById(android.R.id.button1);
		    textView.setTextSize(20);
		    textView.setBackgroundResource(R.color.app_green);
		     
		    View closev =   localMyDialog.findViewById(android.R.id.icon);
		    closev.getLayoutParams().height = dp(context,19);

		    closev.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					localMyDialog.dismiss();
				}
				
		 
				 
			});
}

2. webview 攔截 javascript資源

web_view.setWebViewClient(new WebViewClient() {

           @SuppressLint("NewApi")
           @Override
           public WebResourceResponse shouldInterceptRequest(WebView view, final WebResourceRequest request) {
              final String url = request.getUrl().toString();
              if(url.contains("bottom.js")) 
                 return new WebResourceResponse("text/javascript", "UTF-8", null);

                return super.shouldInterceptRequest(view, new WebResourceRequest() {
                    @Override
                    public Uri getUrl() {
                        return Uri.parse(url);
                    }

                    @SuppressLint("NewApi")
                    @Override
                    public boolean isForMainFrame() {
                        return request.isForMainFrame();
                    }

                    @SuppressLint("NewApi")
                    @Override
                    public boolean hasGesture() {
                        return request.hasGesture();
                    }

                    @SuppressLint("NewApi")
                    @Override
                    public String getMethod() {
                        return request.getMethod();
                    }

                    @SuppressLint("NewApi")
                    @Override
                    public Map<String, String> getRequestHeaders() {
                        return request.getRequestHeaders();
                    }
                });
    }

           @Override
           public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
              android.util.Log.d("webtest", "block:" + url);
              if(url.contains("bottom.js")) {
                 String data = "";
                 java.io.InputStream   input =   new   java.io.ByteArrayInputStream(data.getBytes());
                 return new WebResourceResponse("text/javascript", "UTF-8", data);
              }
              return super.shouldInterceptRequest(view, url);
           }
        });


web_view.setWebChromeClient(new WebChromeClient() {
           @Override
           public void onReceivedIcon(WebView view, Bitmap icon){
                 multiplestatusview.showContent();
                 super.onReceivedIcon(view, icon);
           }
           @Override
           public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
              String message = consoleMessage.message();
              int lineNumber = consoleMessage.lineNumber();
              String sourceID = consoleMessage.sourceId();
              String messageLevel = consoleMessage.message();
              android.util.Log.i("webtest","console:"+message);

              return super.onConsoleMessage(consoleMessage);
           }
           @Override
           public void onProgressChanged(WebView view, int newProgress) {
              android.util.Log.d("webtest","prog:"+newProgress);
              if(newProgress >= 10 && newProgress < 100){
                 view.loadUrl("javascript:" + getInjs());

              }
           }

    });

3. 解決左右滑動,父控件和子控件衝突

myView.setOnTouchListener(new View.OnTouchListener() {
         @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                     v.getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                     v.getParent().requestDisallowInterceptTouchEvent(false);

                    break;
            }
            return false;
        }
    });

4. Fragment 裏面菜單和toolbar整合

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       inflater.inflate(R.menu.appmenu, menu);
       super.onCreateOptionsMenu(menu, inflater);
       final MenuItem item = menu.findItem(R.id.action_search);
    }
    
    public Toolbar initToolbar() {
        setHasOptionsMenu(true);

        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        //toolbar.inflateMenu(R.menu.appmenu);
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        return toolbar;
    }

5. Activity懸浮窗口

public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		chaping();
		WindowManager.LayoutParams params = getWindow().getAttributes(); 
		DisplayMetrics metrics = getResources().getDisplayMetrics();
        int screenWidth = (int) (metrics.widthPixels  );
        int height = (int) (metrics.heightPixels /10);

        params.x = 0;  
        params.height = height;  
        params.width = screenWidth;  
        params.y = 0;  
//        params.gravity = Gravity.LEFT|Gravity.TOP; 
         params.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL| LayoutParams.FLAG_NOT_FOCUSABLE;
      
//	     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

	  	getWindow().setAttributes(params); 
}
相關文章
相關標籤/搜索