版权声明:未经博主允许不得转载

在Android中我们常常用到很多UI控件,如TextViewEditTextImageViewButtonImageButtonToggleButtonCheckBoxRadioButton等等这些可以自己多用就会了。

也会学到一些布局如:LinearLayout 线性布局RelativeLayout 相对布局FrameLayout 帧布局AbsoluteLayout绝对布局TableLayout 表格布局GridLayout 网格布局ConstraintLayout 约束布局

LinearLayout 线性布局:

android:orientation="vertical"
android:orientation="horizontal"

RelativeLayout 相对布局

android:layout_alignParentTop
android:layout_alignParentBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_marginTop
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_margin
android:layout_centerVertical
android:layout_centerHorizontal
android:layout_centerInParent
android:layout_above
android:layout_below
android:layout_alignLeft
android:layout_alignRight
android:layout_toLeftOf
android:layout_toRightOf
android:layout_alignBottom
android:layout_alignTop
android:layout_alignBaseline

TableLayout 表格布局

android:layout_span
android:layout_column
android:collapseColumns
android:shrinkColumns
android:stretchColumns

属性

android:id
android:layout_width
android:match_parent
android:text
android:background
android:textSize
android:textColor
android:inputType
android:hint

结构:

AndroidManifest.xml为整个应用的主配置,清单文件,应用的包名、版本号、组件、权限等信息。

Activity的活动状态:

activity is running:为活动状态
onResume()->running->onPause():为暂停状态
onStart()->onResume()->running->onPause()->onStop():为停止状态
onDestroy():为非活动状态
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
onRestart()

跳转:第二个界面:

private Button mButton;
private String text="dashu"; mButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent data = new Intent();
data.putExtra("text",text);
setResult(2,data);
finish();
}
}

跳转:第一个界面:

@Override
protected void onActivityResult(int requestCode,int resuleCode,Intent data){
super.onActivityResult(requestCode,resultCode,data):
if(requestCode==1&&resultCode==2){
String text = data.getStringExtra("text");
textView.setTextView(text);
}
}

ListView与ArrayAdapter的搭配:

//准备数据源->适配器加载数据源->控件加载适配器
public class MainActivity extends AppCompatActivity{
private ListView listView;
private ArrayAdapter<String> list_adpater; @Override
protected void onCreate(Bundle savedInsstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=findViewById(R.id.listView); //1
String data[] = {"dashu1","dashu2","dashu3"};
//2
list_adpater = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,data);
//3
listView.setAdapter(list_adapter); }
}

ListView与SimpleAdapter的搭配:

//列表项
//布局
<ListView
android:id="@android:id/app_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
//单个列表项
<ImageView
android:id="@+id/icon_image_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/title_text_view"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="@string/app_name"
android:textSize="24sp"/>
private ListView listView;
private List<Map<String,Object>> dataList;
private SimpleAdapter simple_adapter;
//1.
dataList = new ArrayList<>();
dataList=getData();
private List<Map<String,Object>> getData(){
for(int i = 0;i<10;i++){
Map<String,Object> map = new HashMap<>();
map.put("image",R.drawable.images1);
map.put("text",i);
dataList.add(map);
}
return dataList;
}
//2.
simple_adapter = new SimpleAdapter(this,dataList,R.layout.item,new String[]{"image","text"},new int[]{"R.id.imageView","R.id.imageView"});
//3.
listView.setAdapter(simple_adapter);

To:

public class App{
private String appName;
private int appId;
App(String appName,int appId){
}
}
//
public class AppAdapter extentds ArrayAdapter<App>{
...
App app = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(...);
}
//
//获取视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//创建ViewHolder
ViewHolder viewHolder = null;
//如果为空
if (convertView == null){
//mInflater.inflate(渲染布局,parent , false)
convertView = mInflater.inflate(R.layout.item_msg,parent,false);
viewHolder = new ViewHolder();//创建类对象
//视图
viewHolder.mIvImg = convertView.findViewById(R.id.id_iv_img);
viewHolder.mTvTitle = convertView.findViewById(R.id.id_tv_title);
viewHolder.mTvContent = convertView.findViewById(R.id.id_tv_content); convertView.setTag(viewHolder);// convertView.getTag(); }else {
viewHolder = (ViewHolder) convertView.getTag();// convertView.getTag();
}
//获取数据位置
Message msg = mDatas.get(position);//数据集合 private List<Message> mDatas;
viewHolder.mIvImg.setImageResource(msg.getImgResId());//获取图片位置
viewHolder.mTvTitle.setText(msg.getTitle());//获取标题
viewHolder.mTvContent.setText(msg.getContent());//获取内容 return convertView;
} public static class ViewHolder{
//静态内部类,类调用
ImageView mIvImg;
TextView mTvTitle;
TextView mTvContent;
}
//or
App app = getItem(position);
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder=new ViewHolder();
viewHolder.imageView= view.findViewById(R.id.app_image);
viewHolder.textView= view.findViewById(R.id.appl_name);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder = (ViewHolder) view.getTag();
...
return view;
}

To:

private List<Map<String,Object>> dataList;
private SimpleAdapter simpleAdapter;
private void getData(){
for(int i=0;i<##;i++){
Map<String,Object> map = new HashMap<>();
map.put("",##);
dataList.add(##);
}
}

ProgressBarmax,progress,secondaryProgress

style="?android:attr/progressBarStyleHorizontal"
style="?android:attr/progressBarStyleLarge"
style="?android:attr/progressBarStyle"
style="?android:attr/progressBarStyleSamll"
int first=progressBar.getProgress();
int second=progressBar.getSecondaryProgress();
int max = progress.getMax();
progressBar.incrementProgressBy(5);
progressBar.incrementSecondaryProgressBy(5); progress.incrementProgressBy(-5);
progressBar.incrementSecondaryProgressBy(-5);
ProgressDialog progressDialog = new ProgressDialog(this);
//
progressDialog.setProgresssStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("");
progressDialog.setIcon("");
//
progressDialog.setMax(100);
progressDialog.incrementProgressBy(20);
//
progessDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定",new DialogInterface.OnClickListener(){
...
});
progressDialog.setCancelable(true);
//
progressDialog.show();

SeekBarOnSeekBarChangeListener

<SeekBar>
...
android:max="100"
android:progress="40"
</SeekBar>
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

});

日历选择器:OnDateChangedListener

//DataPicker日历选择器
private DataPicker dataPicker;
private Calendar calendar;
private int year,day,hour,minute;
//
calendar = Calendar.getInstance();
//
year = calendar.get(Calendar.YEAR);
...
minute = calendar.get(Calendar.MINUTE); //DataPickerDialog-OnDateSetListener()
new DatePickerDialog(this, newDatePickerDialog.OnDateSetListener(){
...
},year,calendar.get(Calendar.MONTH),day).show();
//TimePicker-setOnTimeChangedListener()
//TimePickerDialog-OnTimeSetListener
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener(){
....
},hour,minute,true).show();

Meau

//OptionsMenu,SubMenu,ContextMenu
//
<menu>
<item android:id="@+id/item1"
android:title="1"
</item>
<item android:id="@+id/item12"
android:title="12"
</item>
//
<item android:id="@+id/play" android:title="玩">
<menu>
<item android:id="" android:title="下棋"/>
<item .../>
</menu>
</item>
//
雷同SubMenu
//
getMenuInflater().inflate();
onCreateOptionsMenu();
onCreateContextMenu();
//动态
onCreateOptionsMenu(Menu menu){
menu.add(1,1,1,"1");
menu.add(1,2,1,"2");
return super.onCreateOptionsMenu(menu);
}
onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo){
//menu.setHeaderTitle();
//menu.setHeaderIcon();
menu.add(1,1,1,"1");
menu.add(1,2,1,"2");
...
super.onCreateContextMenu(menu,v,menuInfo);
}
//
onCreateOptionsMenu(Menu menu){
//SubMenu play=menu.addSubMenu();
play.setHeaderTitle();
play.setHeaderIcon();
play.add();
play.add();
SubMenu play2=menu.addSubMenu();
paly2.add(2,1,1,"");
play2.add(2,2,1,"");
return super.onCreateOptionsMenu(menu);
}
//
ContextMenu
ListView = findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);
registerForContextMenu(listView); //OnOptionsItemSelected
OnOptionsItemSelected(MenuItem item){
if(item.getGroupId() == 1){
switch(item.getItemId()){
case 1:
break;
case 2:
break;
}
}
...
}

Notification 通知

图标(SmallIcon)
标题(ContextTitle)
内容(ContextText)
时间(When)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//
builder.setSmallIcon();
...
builder.setDefaults(Notification.DEFAULT_ALL);
//
PendingIntent类-setContentIntent()
NotificationManager去完成发送和取消通知的事情

Dialog 对话框

//setPositiveButton()和setNegativeButton():添加确定和取消按钮
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle();
...
builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
...
}
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
...
}
Dialog dialog = builder.create();
dialog.show();
//setSingleChoiceItems()
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle();
builder.setIcon();
builder.setSingleChoiceItems(data,0,new DialogInterface.OnClickListener(){
...
});
Dialog dialog = builder.create();
dialog.show();

WebView

//uri , setAction , setData
Uri uri = Uri.parse(url);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
<WebView>
</WebView>
//
webView = findViewById(R.id.webView);
webView.loadUrl(url);
//
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//
webView.setWebViewClient(new WebViewClient(){
...
}); //
webView.setWebChromeClient(new WebChromeClient(){
...
//
if(dialog != null && dialog.isShowing()){
dialog.dismiss():
dialog = null;
}
...
//
if(dialog == null){
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle();
...
dialog.show();
}else{
dialog.setProgress(newProgress);
}
}

Fragment:为了解决Android碎片化

Fragment is added
onAttach()
onCreate()
onCreateView()
onActivityCreated()
onStart()
onResume()
Fragement is activite
onPause()
onStop()
onDestroyView();
onDestroy()
onDetach()
Fragment is destroyed
onCreate()
onCreateView()
onActivityCreated()
onStart()
onResume()
onPause()
onStop() onStart()
onResume()
onPause()
onStop()
onDestroyView()
onDestroy()
onDetach()
//
MyFragment fragment = new MyFragment();
FragementManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); beginTransaction.add(R.id.layout,fragment);
beginTransaction.commit(); //
getFragmentManager().beginTransaction().add(R.id.layout,fragment).commit(); //myFragment = (MyFragment)getFragmentManager(). findFragmentById(R.id.fragment);
//Fragment.java
Toast.makeText(getActivity(),"dashu",Toast.LENGTH_SHORT).show();

File,SharedPreferences,SQLite

private SQLiteDatabase db;
public class MyHelper extends SQLiteOpenHelper{
public static String CREATE_TABLE="create table student("
+ " _id integer primary key autoincrement,"
+ "name varchar(20),"
+ "age integer,"
+ "sex varchar(2)";
public Context mContext; public MyHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version){
super(context,name,factory,version);
mContext=context;
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL();
} //
Cursor cursor=db.query("student",null,null,null,null,null,null);
if(cursor!=null){
while(cursor.moveToNext()){ }
}

ContentProvider

Android四大组件之一,为存储和获取数据提供统一的接口。

自定义类继承ContentProvider,通过android:authorities属性授权。ContentResolver内容处理者,提供ContentProvider中同名同参的增删改查方法。

boolean onCreate()
String getType(Uri uri)
Cursor query()
Uri insert()
int update()
int delete()
//URI : 完整的uri路径
//
ContentProvider内容提供者
ContentResolver内容解析者

Spinner下拉框

<RelativeLayout>
<Spinner
android:id="@+id/spinner" ...
</RelativeLayout>
//android:entries="@array/list"
<resources>
<array name="list">
<item>1</item>
<item>2</item>
</array>
</resources>
public class MainActivity extends AppCompatActivity{
private Spinner mSpinner;
...
//
final String[] arrays = getResources().getStringArray(R.array.list);
//
ArrayAdapter<String> mSpinnerAdaper=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,arrays);
mSpinner.setAdapter(mSpinnerAdaper);
//setOnItemSelectedListener()
...
}

如果觉得不错,那就点个赞吧!❤️

编辑 :达叔

定位:分享 Android&Java 知识点

Android之控件与布局,结构知识点,基础完结的更多相关文章

  1. Android 手机卫士--自定义组合控件构件布局结构

    由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...

  2. Android编程 控件与布局

    控件和布局的继承结构 常用控件 1.TextView <?xml version="1.0" encoding="utf-8"?> <Line ...

  3. android之控件与布局

    基本控件:TextViewButtonEditTextImageViewAlertDialog.BubliderProgressDialog 四种基本布局的特殊属性: LinerLayout andr ...

  4. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

  5. Android:控件布局(线性布局)LinearLayout

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

  6. Android:控件布局(相对布局)RelativeLayout(转)

    相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above----------位于给定DI控件之上android:layout_below ------ ...

  7. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  8. Android基本控件之Menus

    在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...

  9. 背水一战 Windows 10 (38) - 控件(布局类): Panel, Canvas, RelativePanel, StackPanel, Grid

    [源码下载] 背水一战 Windows 10 (38) - 控件(布局类): Panel, Canvas, RelativePanel, StackPanel, Grid 作者:webabcd 介绍背 ...

随机推荐

  1. javascript:控制台详解

    javascript工具——浏览器控制台详解  大神这篇博客是写在2011年,主要介绍 “Firefox” 浏览器插件 “Firebug” 的操作,如今主流浏览器对控制台都已经提供了很好的支持.我自己 ...

  2. python中的多进程与多线程(一)

    进程是一个执行中的程序,每个进程有自己的地址空间.内存.数据栈以及其他用于跟踪执行的辅助数据.操作系统管理其上所有进程,并合理分配时间. 进程也可以通过fork或spawn派生新的进程,每个新进程有自 ...

  3. R语言-图的要素颜色

    1.设置图形要素的颜色 Plot函数中,使用col=参数来决定要素的颜色 如果不指定plot type,颜色加在散点上,如果指定了plot type,例如line,则颜色加在线上 其它函数,例如bar ...

  4. autoperfixer 版本配置

    { "private": true, "dependencies": { "autoprefixer": "^9.3.1" ...

  5. Jmeter连接MySQL和SQL Server配置(转载)

    在使用jmeter做性能或自动化测试的时候,往往需要直接对数据库施加压力,或者某些参数只能从数据库获取,这时候就必须使用jmeter连接数据库. 2 下载对应的驱动包 mysql驱动: (mysql- ...

  6. django 模板使用

    1 配置 在工程中创建模板目录templates. 在settings.py配置文件中修改TEMPLATES配置项的DIRS值: TEMPLATES = [ { 'BACKEND': 'django. ...

  7. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. stark组件开发之关键搜索

    - 模糊搜索: 在页面生成一个表单.  以get 方式, 将数据提交到.当前查看页面. 后台接收数据,然后进行筛选过滤. 着个也需要,用户自定制!   定义一个  search_list  这个值,默 ...

  9. 13. pt-ioprofile

    pt-ioprofile 直接运行即可,对系统有影响,慎用!!! read:从文件中读出数据.要读取的文件用文件描述符标识,数据读入一个事先定义好的缓冲区. write:把缓冲区的数据写入文件中. p ...

  10. 为docker配置HTTP代理服务器

    背景: node1不能访问外网, node2可以访问外网,node1通过node2的代理服务来访问外网. 1. node1不能访问外网 vim /etc/resolv.conf 注释掉DNS配置文件 ...