android中的小布局

    xiaoxiao2026-03-18  5

    一:对于android开发中需要加载网页

      1.首先在layout布局文件中加载

    <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent"></WebView>

      2.在清单文件加入网络访问权限(写在application之外)

    <!--访问网络权限--><uses-permission android:name="android.permission.INTERNET"/>

      3.mainActivity中实现

         (1)可以先静态绑定一个网址如: private String url="http://www.baidu.com";

         (2)通过findVIewById找到WebView对象,View.loadUrl(url);//加载外部网页 

    若需要在本应用界面而不是跳转到第三方浏览器去显示网页操作如下:

    //覆盖webView默认通过第三方或者是系统浏览器打开网页的行为。使得网页可以在webView中打开webView.setWebViewClient(new WebViewClient(){@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {//返回值为true时控制网页在webViewh中打开,如果falseze通过第三方或者是系统浏览器打开网页 //return super.shouldOverrideUrlLoading(view, url); view.loadUrl(url); return true; }//webViewClient帮助webView去处理一些页面控制和请求通知});

       (3)可以启用javaScrip

    webView;true;//支持javaScrip

    //webView加载页面使用优先缓存加载

    //是否需要进行缓存

    settings.setCacheMode(WebSettings. LOAD_CACHE_ELSE_NETWORK ) ; //缓存 //settings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不缓存

      (4)可以对网页进度进行实时监控,用对话框进度条

    webView.setWebChromeClient(new WebChromeClient(){@Override public void onProgressChanged(WebView view, int newProgress) {//newProgress是1到100的整数,课做为精度 if(newProgress==100){ //加载完成 (用自定义函数)关闭progressDialog closeDialog(); }else{//加载中(用自定义函数) 打开progressDialog openDialog(newProgress); }super.onProgressChanged(view, newProgress); } });

    private void openDialog(int newProgress) {if(proDialog==null) {proDialog = new ProgressDialog(MainActivity.this);//创建进度条对话框对象 proDialog.setTitle("加载中..."); proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); proDialog.setProgress(newProgress); proDialog.setIndeterminate(false); proDialog.show(); }else{proDialog.setProgress(newProgress); } }private void closeDialog() {if(proDialog!=null&&proDialog.isShowing()){proDialog.dismiss(); proDialog=null; } }

       (5)当需要在点击手机物理返回键时不会退出应用,需要修改返回键

    //改写手机物理逻按键返回的逻辑public boolean onKeyDown(int keyCode, KeyEvent event){//可以获得当前网址 //Toast.makeText(MainActivity.this,webView.getUrl(),Toast.LENGTH_SHORT).show(); if(keyCode==KeyEvent.KEYCODE_BACK){//点击手机返回键 if(webView.canGoBack()){ //判断是否可以返回 webView.goBack();//返回上衣界面 return true; } }else{ System.exit(0);//退出程序 }return super.onKeyDown(keyCode,event);}

    二.对于进度条的使用:

      1.首先在layout布局文件中进行加载ProgressBar

    <ProgressBar

            style="?android:attr/progressBarStyleSmall"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:id="@+id/progressBar"

            android:layout_alignParentLeft="true"

            android:layout_alignParentStart="true" />

      (1)style="?android:attr/progressBarStyleSmall"  //表示圆环小型进度条

      (2)当不写style时默认是圆环正常大小的

       (3)style="?android:attr/progressBarStyleLarge"   //圆环大型

      (4)style="@android:style/Widget.ProgressBar.Horizontal"  //条形横向

       2.可以在onCreat方法的SetContentView之前设置:(好像现在可以不用)

    //启动窗口特征,启动带进度和不带进度的requestWindowFeature(Window.FEATURE_PROGRESS);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

      3.给进度条textView显示进度

    int first=progress.getProgress(); //获取第一进度int second=progress.getSecondaryProgress(); //获取第二进度int max =progress.getMax(); //获取最大进度teextView.setText(""+(int)(first/(float)max*100)+"%"+" "+(int)(second/(float)max*100)+"%");

       4.也可以用对话框进度条  ProgressDialog对象 (.java文件中实现)

    //1.新建progressDialog对象     对话框进度条

      proDialog=new ProgressDialog(MainActivity.this);

      //2.设置风格为横向

       proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

     //3.设置标题

      proDialog.setTitle("java工作室!");

     //设置显示文本

     proDialog.setMessage("欢迎来到中国");

     //设置图标

     proDialog.setIcon(R.drawable.img_8);

     /*

    设置进度条属性

    */

    //设置最大进度

    proDialog.setMax((int)totalspace);

     //设置初始化已经增长的进度

    proDialog.incrementProgressBy((int)usablespace);

     //设置进度条是明确显示进度的

    progress.setIndeterminate(false);  //显示进度

    //设置一个确定俺按钮在对话框上。(需要时Activity继承 onclick)

    proDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定",new DialogInterface.OnClickListener(){

    @Override

    public void onClick(DialogInterface dialogInterface, int i) {

    Toast.makeText(MainActivity.this,"欢迎来到数据库工作室",Toast.LENGTH_SHORT).show();

     }

    });

    //是否可以通过放回按钮退出对话框

    proDialog.setCancelable(true);

    //显示ProgressDialog

    proDialog.show();  //一定记得show 出来,否则不会有对话框

    三.对手机内存空间的获取

    //获得Sdcard的总和可用空间File file= Environment.getExternalStorageDirectory();long totalspace=file.getTotalSpace();long usablespace=file.getUsableSpace();//转化数据类型String formatTotalSpace= Formatter.formatFileSize(this,totalspace);String formatusablespace=Formatter.formatFileSize(this,usablespace);

    四.GriView视图界面的使用

       (1)首先在主布局中加载GriView

    <GridView android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:numColumns="3"

    android:id="@+id/gridView" />

    <!-- android:horizontalSpacing="" 两列之间的间距android:verticalSpacing="" 两行之间的间距android:numColumns="" 每一行显示几列 auto_fit表示自动适配-->

       (2)再新创建一个布局文件 item.xml

    在当中添加控件  <ImageView/> 和<TextView/>

       (3)在MainActivity中加载SimpleAdapter 适配器

    主要步骤:

    /* 1. 准备数据 2, 新建适配器(simpleadapter) 3.GriView加载适配器 4.GriView 适配事件监听器(OnIte吗ClickListener) */ 适配器: private GridView gridView;private List<Map<String,Object>> dataList;private SimpleAdapter adapter;

    创建资源:

    //icon存放图片 图片的id都是int型private int [] icon={R.drawable.img_1,....}; //存放字private String [] iconName={"通讯录","课堂",......};

    gridViewgridView;dataListnew ,;adapternew this,,item, new "image","text",new intimageView,textView;gridViewadapter;//夹加载适配器gridViewthis;

    //数据包装

    public List<Map<String ,Object>> getDataList(){for (int i=0;i<icon.length;i++) { Map<String ,Object> map=new HashMap<String ,Object>(); map.put("image",icon[i]); map.put("text",iconName[i]); dataList.add(map); }return dataList;}

    //点击事件:

    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    //i代表iconName中的位置从0开始的 (long l)表示id 从0开始

    Toast. makeText( this, "我是"+ iconName[i] + " "+l , Toast. LENGTH_SHORT).show() ;}

    最新回复(0)