资讯专栏INFORMATION COLUMN

Android之控件与布局,结构知识点,基础完结

honhon / 1447人阅读

摘要:也会学到一些布局如线性布局,相对布局,帧布局,绝对布局,表格布局,网格布局,约束布局。线性布局相对布局表格布局属性结构为整个应用的主配置,清单文件,应用的包名版本号组件权限等信息。

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

在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 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的搭配:

//列表项
//布局

//单个列表项

private ListView listView;
private List> dataList;
private SimpleAdapter simple_adapter;
//1.
dataList = new ArrayList<>();
dataList=getData();
private List> getData(){
 for(int i = 0;i<10;i++){
   Map 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 = 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 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> dataList;
private SimpleAdapter simpleAdapter;
private void getData(){
 for(int i=0;i<##;i++){
  Map 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


  ...
  android:max="100"
  android:progress="40"
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
//



//

 
  
  
 

//
雷同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(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 = 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下拉框


 
//android:entries="@array/list"

  
   1
   2
  
public class MainActivity extends AppCompatActivity{
 private Spinner mSpinner;
  ...
   //
   final String[] arrays = getResources().getStringArray(R.array.list);
  //
  ArrayAdapter mSpinnerAdaper=new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,arrays);
    mSpinner.setAdapter(mSpinnerAdaper);
   //setOnItemSelectedListener()
   ...
}

如果觉得不错,那就点个赞吧!❤️
编辑 :达叔
定位:分享 Android&Java 知识点

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/2959.html

相关文章

  • Android 自定义 View - 收藏集 - 掘金

    摘要:在本篇文安卓自定义进阶分类和流程掘金自定义分类与流程经历过前面三篇啰啰嗦嗦的基础篇之后,终于到了进阶篇,正式进入解析自定义的阶段。 Android 从 0 开始自定义控件之 View 的 draw 过程 (九) - Android - 掘金转载请标明出处: http://blog.csdn.net/airsaid/... 本文出自:周游的博客 ... Andriod 从 0 开始自定义...

    AndroidTraveler 评论0 收藏0
  • Android 自定义View - 收藏集 - 掘金

    摘要:在本篇文安卓自定义进阶分类和流程掘金自定义分类与流程经历过前面三篇啰啰嗦嗦的基础篇之后,终于到了进阶篇,正式进入解析自定义的阶段。 这交互炸了(二):爱范儿是如何让详情页缩小为横向列表的 - 掘金本文同步自wing的地方酒馆 写在前面:写这段话的时候,已经是夜里3点了。别问我为什么这么拼,一切为了与你分享干货!!!! 不要太感动,擦擦眼泪继续往下看。 本开源库链接 Expandable...

    yanbingyun1990 评论0 收藏0
  • 网易严选App感受Weex开发(已完结

    摘要:如果你尚不了解,并想简单入门,可以阅读整理快速入门笔记网易严选感受开发什么都不说,先给你感受下的效果。此处对寄有厚望单位中的所有属性值的单位均为,也可省略不写,系统会默认为单位。 showImg(https://segmentfault.com/img/remote/1460000012869672); 自打出生的那一天起,Weex 就免不了被拿来同 React Native「一决高下...

    jaysun 评论0 收藏0
  • 一个老鸟发的公司内部整理的 Android 学习路线图

    摘要:一个老鸟发的公司内部整理的学习路线图年月日阅读数发了一篇一个老鸟也发了一份他给公司内部小伙伴整理的路线图。另一份开发学习路线图。看完这本书后,小明对的历史结构代码规范等都有了一个大概的了解,并且,小明已经可以写出一些简单的了。一个老鸟发的公司内部整理的 Android 学习路线图 2017年09月12日 17:13:27 阅读数:20449   jixiaohua发了一篇一个老...

    miya 评论0 收藏0
  • Material Design控件使用(完结篇)

    摘要:本文整合前面四篇的控件,再结合豆瓣读书的,做了一个搜索书籍和查看书籍信息的。项目依赖库书籍列表使用了和进行布局。 本文整合前面四篇的控件,再结合豆瓣读书的API,做了一个搜索书籍和查看书籍信息的Demo。 项目依赖库 groovydependencies { compile fileTree(dir: libs, include: [*.jar]) compile ...

    ISherry 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<