Android开发之布局和数据库学习
来源:互联网 发布:网络购彩 恢复了吗 编辑:程序博客网 时间:2024/06/08 08:00
第二部分:五大布局和数据储存
关于布局,我就简单的总结一些特性,在实际开发中,布局感觉并没有特别的重视,不同场景下应用不同的布局方式,是个人开发习惯。
1. 线性布局(LinearLayout)
LinearLayout是线性布局的控件,它包含的子控件以横向或竖向的方式排列。
属性:
android:orientation = “vertical”
android:gravity = “center”–它子类的xy的位置
(属性值: center_vertical:垂直(Y轴)居中;center_horizontal:水平(X轴)居中;center:水平垂直都居中;right:子类位于右边;left:子类位于左边;bottom:子类位于下面)
android:layout_gravity = “bottom”–指本身在当前父容器的XY的位置
android:layout_weight = “1”—指本身控件占用当前父容器的一个比例
2.相对布局(RelativeLayout)
这是我自己本身最喜爱用的一种布局方式,优点在于灵活,自由,自己设计排列方式。
3.帧布局(FrameLayout)
在这个布局中,所有子元素都不能被指定放置的位置,它们通通放于这块区域的左上角,并且后面的字元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。
4.绝对布局(AbsoluteLayout)
又可以叫做坐标布局,可以直接指定子元素绝对位置(xy)
PS:但不建议这么做,在开发的时候,各种手机尺寸差别大,屏幕自适应方面效果差;
5.表格布局(TableLayout)
以行列的形式管理子控件,每一行为为一个TableRow的对象,当然也可以是一个View的对象。
接下来就是Android开发中必不可少的关于数据库方面的学习,Android有四种存储方式:
1. SharedPreferences
2. SQLite
3. Content Provider
4. File
Shared Preferences (保存用户名,保存密码 ):
1. 是一种轻型的数据存储方式
2. 本质是基于XML文件存储key-value键对数据
3. 通常用来存储一些简单的配置信息
本身只能获取数据而不支持存储和修改,存储修改时通过Editor 对象实现:
存储步骤
(1) 获得share prefence对象
(2) 获得shared prefence,Editor 对象
(3) 通过Editor接口的put Xxx方法保存key-value 对其中XXX表示不同的数据类型
(4) 通过Editor接口的commit方法保存key-value对
实例操作代码如下:
学习资源感谢慕课网
package com.demo.top.sharepreferencedemo01;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// SharedPreferences pre = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); SharedPreferences pref = getSharedPreferences("myPref",MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.putString("name","Andy"); editor.putInt("age",22); editor.putLong("time", System.currentTimeMillis()); editor.putBoolean("default",true); editor.commit(); System.out.println(pref.getString("name","")); System.out.println(pref.getInt("age",0)); }}
SQLite:
是由D.Richard.Hipp 由C编写,开源嵌入式数据库引擎。
SQLite的数据类型:
Null Interger Real Text BLOB
在Android中如何使用SQLite:
(1) SQLite DataBase
1. 提供了一些管理SQLite数据库的类
2. 提供了一些创建,删除,执行SQL命令,并执行其他常见DataBase数据库管理任务的方法
3. 每个数据库名字是唯一的
(2) SQLite OpenHelper
SQLite DataBase的帮助类,用来管理数据库的创建和版本更新,一般是建立一个类继承它,并重写onCreate()和onUpgrade()方法
方法:onCreate(SQLite DataBase db)—创建DB时调用
onUpgrade(SQLite DataBase db,int oldVersion,int newVersion)
getReadableDatabase() —创建或打开一个只读数据库
getWritableDatabase() —创建或打开一个读写数据库
使用内置函数操作数据库:
Content Values:这个类是用来存储可以被ContentResolver处理的值。
ContentValues values = new ContentValues();//类似hashMap中的键值对
values.put(“name”,“张三”)//执行对应的Sql操作
实例操作代码如下:
学习资源感谢慕课网
package com.demo.top.sqlite;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * 使用sql语句来操作数据库 */// //每个程序都有自己的数据库 默认情况下是各自互相不干扰// //创建一个数据库 并且打开// SQLiteDatabase db = openOrCreateDatabase("user.db",MODE_PRIVATE,null);// db.execSQL("create table if not exists usertb(_id integer primary key autoincrement,name " +// "text not null,age integer not null,sex text not null)");// db.execSQL("insert into usertb(name,sex,age) values('桐宝','女',23)");// db.execSQL("insert into usertb(name,sex,age) values('阿姣','女',22)");// db.execSQL("insert into usertb(name,sex,age) values('阿同','男',23)");//// Cursor c = db.rawQuery("select * from usertb",null);// if (c!=null){// while (c.moveToNext()){// Log.i("info","_id:"+c.getInt(c.getColumnIndex("_id")));// Log.i("info","name:"+c.getString(c.getColumnIndex("name")));// Log.i("info","age:"+c.getInt(c.getColumnIndex("age")));// Log.i("info","sex:"+c.getString(c.getColumnIndex("sex")));// Log.i("info","!!!!!!!!!!");// }// c.close();// }// db.close(); /** * 使用内置函数操作数据库 */// SQLiteDatabase db = openOrCreateDatabase("stu.db",MODE_PRIVATE,null);// db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name " +// "text not null,age integer not null,sex text not null)");// ContentValues values = new ContentValues();// values.put("name","阿姣");// values.put("sex","女");// values.put("age",22);// long rowId = db.insert("stutb",null,values);// values.clear();// values.put("name","阿同");// values.put("sex","男");// values.put("age",23);// db.insert("stutb",null,values);// values.clear();// values.put("name","阿桐");// values.put("sex","男");// values.put("age",23);// db.insert("stutb",null,values);// values.clear();// values.put("sex","女");// db.update("stutb",values,"_id>?",new String[]{"1"});// db.delete("stutb","name like ?",new String[]{"%桐%"});// Cursor c =db.query("stutb",null,"_id>?",new String[]{"0"},null,null,"name");// if (c!=null){// String[] columns = c.getColumnNames();// while (c.moveToNext()){// for (String columnName:columns){// Log.i("info",c.getString(c.getColumnIndex(columnName)));//// }// }// c.close();// }// db.close(); /** * 使用SQLiteOpenHelper帮助类完成 * */ DBOpenHelper helper = new DBOpenHelper(MainActivity.this,"stu.db");// helper.getReadableDatabase();//获取一个只读的数据库 只能查询 不能写入 不能更新 SQLiteDatabase db = helper.getWritableDatabase(); Cursor c = db.rawQuery("select * from stutb",null); if (c!=null){ String[] columns = c.getColumnNames(); while (c.moveToNext()){ for (String columnName:columns){ Log.i("info",columnName+ ":"+c.getString(c.getColumnIndex(columnName))); } } c.close(); } db.close(); }}
package com.demo.top.sqlite;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;/** * Created by Administrator on 2017/1/18. */public class DBOpenHelper extends SQLiteOpenHelper { public DBOpenHelper(Context context, String name) { super(context, name,null,1); } public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override//首次创建数据库的时候调用 一般可以把建库 建表的操作 public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name " + "text not null,age integer not null,sex text not null)"); db.execSQL("insert into stutb(name,sex,age) values('桐宝','女',23)"); } @Override //当数据库的版本发生变化的时候,会自动执行 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}
Android中的文件存储(大量数据):
File
-Activity的openFileOutput()方法可以用与把数据输出到文件来
-创建的文件保存在/data/data//files目录
-实现过程在Java中保存数据到文件中是一样的。
实例操作代码如下:
学习资源感谢慕课网
package com.demo.top.filedemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class MainActivity extends AppCompatActivity { private EditText mEditText; private Button mButton; private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// File file = new File("/mnt/sdcard/test");// if(!file.exists()){// try {// file.createNewFile();// } catch (IOException e) {// e.printStackTrace();// }// }else {//// Toast.makeText(MainActivity.this,"文件已经存在",Toast.LENGTH_LONG);// }//// file.delete();// File file = this.getFilesDir();//这个目录是当前应用程序默认的数据存储目录// Log.i("info",file.toString());// File file = this.getCacheDir();//这个目录是当前应用程序默认的缓存// // 文件存放位置,把一些不是特别非常重要的文件在此处创建使用,// // 如果手机的内存不足的时候,系统会自动删除APP的cache目录的数据// Log.i("info",file.toString()); // /data/data/<包名>/app_imooc// File file = this.getDir("imooc",MODE_PRIVATE);// Log.i("info",file.toString());// File file = this.getExternalCacheDir();//可以得到外部的存储位置// // ,该位置的数据跟内置的使用是一样的,如果APP卸载了,这里面的数据也会自动清除掉// //如果说开发者不遵守这样的规则,不把数据放入data/data/<包名>// // /mnt/sdcard/Android/data/<包名> 卸载之后数据将不会自动清除掉 将会造成所谓的数据垃圾// Log.i("info",file.toString()); mEditText = (EditText) findViewById(R.id.editText); mButton = (Button) findViewById(R.id.write); mTextView = (TextView) findViewById(R.id.contentvalue); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { WriteFiles(mEditText.getText().toString()); mTextView.setText(readFiles()); } }); } //保存文件内容 public void WriteFiles(String content) { FileOutputStream fos = null; try { fos = openFileOutput("a.txt",MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); } } //读取文件内容 public String readFiles(){ String content = null; try { FileInputStream fis = openFileInput("a.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) != -1) { baos.write(buffer, 0, len); } content = baos.toString(); fis.close(); baos.close(); }catch (IOException e) { e.printStackTrace(); } return content; }}
- Android开发之布局和数据库学习
- Android开发学习之布局
- 【Android开发学习23】界面布局之线性布局LinearLayout
- 【Android开发学习25】界面布局之相对布局RelativeLayout
- Android开发之自定义布局和控件
- Android开发学习之路--UI之自定义布局和控件
- android 开发之布局
- android开发之布局
- Android开发学习之路--UI之基本布局
- Android开发学习之路--性能优化之布局优化
- 学习android之布局
- Android学习之布局
- Android开发学习之SQLite数据库初探
- Android应用开发学习笔记之表格布局
- Android应用开发学习笔记之帧布局
- Android应用开发学习笔记之相对布局
- Android开发学习之卡片式布局的简单实现
- android开发学习之-----界面布局 TableLayout + TableRow
- InfluxDB
- [HDU] 1010 Tempter of the Bone [DFS][奇偶剪枝]
- c/c++ struct的定义、声明、对齐方式
- ruby中的变量及其作用域
- Oracle系统包——dbms_transaction用法
- Android开发之布局和数据库学习
- SQLite入门(一) 之 C/C++ 接口 API
- Easier Done Than Said?杭电ACM1039
- phpstorm 2016.2.x 最新版激活方法
- hadoop 2.7.3在ubantu Linux下的集群(3台机)
- Hdu 2966 In case of failure kd-tree模板题
- Android studio NDK成长记录(五)将C中的Log打到Logcat
- debug环境下动态注入代码 for iOS
- apk瘦身指南