Android学习day03

先序

我看nmdGit。

第三章-UI开发

3.1 如何编写程序界面

1、可视化编辑器。不推荐
2、XML,推荐

3.2常用控件

3.2.1 TextView
这个环节不知道写什么,就记录一些常用的属性吧
id,layout_width、height,gravity,textSize,textColor
match_parent 让父布局来决定子布局
wrap_content 大小刚好能包裹住内容大小
3.2.2 Button
基本和textview一样,然后自动支持文字大写,使用textAllCaps关闭,
3.2.3 EditText
输入框,提示性文字 hint
最大输入问题,宽度如果设置为wrap_content,会有无限输入的问题,所以可以引入maxLines,单位为个数,意思为最大输入数为n行,当超过时文字就向上滚动
非UI问题:
String text = editText.getText().toString()
复习一下Toast.
Toast.makeText(MainActivity.this?哪个界面,一般是当前界面,text?显示文字,Toast.LENGTH_SHORT?显示时间).show():
3.2.4 ImageView
显示图片的空间,通常需要在res下先创建一个drawable目录,一开始如果有,但是并没有指定相应的具体分辨率,所以在创建一个drawable-xhdpi
引用src
非UI:
imageView = findViewById(R.id.image_view);
imageView.setImageResource(R.drawable.img_2);
3.2.5 ProgessBar
进度条,用可显示性实现加载显示
visibility: visible ,invisible,gone
样式
style="?"
非UI:get/setVisibility方法
绑定按钮,然后通过get然后set决定可见性,然后也会有可能有进度条式的,这样可以使用一些其他的方法。

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (progressBar.getVisibility() == View.GONE) {
//                    progressBar.setVisibility(View.VISIBLE);
//                }else{
//                    progressBar.setVisibility(View.GONE);
//                }

                int progress = progressBar.getProgress();
                progress += 10;
                progressBar.setProgress(progress);
            }
        });

3.2.5 AlertDialog
当前界面上弹出一个对话框,置顶于当前面所有界面元素之上,一般用于提示一些非常重要的内容或者警示信息。

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {![在这里插入图片描述](https://img-blog.csdnimg.cn/20191222171240737.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pvX19fam8=,size_16,color_FFFFFF,t_70)

                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("This is a Dialog");
                dialog.setMessage("Important Message");
                dialog.setCancelable(false);
                dialog.setPositiveButton("确认",new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.setNegativeButton("取消",new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }

Android学习day03_第1张图片
3.2.7 ProgressDialog

ProgressDialog和AlertDialog相似,都是弹出一个框,但是ProgressDialog会显示一个进度条,表示该进程会耗时,需要等待一段时间,

3.3 四种基本布局

3.3.1 线性布局 LinearLayout
控件在线性方向上依次排列,android:orientation=“horizontal(水平) / vertical(竖直)”
Android学习day03_第2张图片
Android学习day03_第3张图片
一个重要的属性android:layout_weight,这个属性非常好用,用了这个之后,控件的宽度就不在随width控制了,而由weight来控制,你可以输入1,1来实现1:1的比例,用比例来算,这样也解决了绝对定位的问题。
假如要实现一个搜索界面,需要一个editText和一个button,可以button使用width实现包裹内容大小,然后让搜索框使用weight = 1,这样可以完美的沾满屏幕,并且适配性强。

3.3.2 相对布局 RelativeLayout
通过定位的方法让控件出现在布局的任何位置
常用属性:
控件相对于父局
android:layout_alignParentLeft / Top / Right / Bottom / centertInParent
布局左上右小角、中间
控件相对于控件
android:layout_above / below 相对控件上/下
android:layout_toRightOf / toLeftOf 相对控件 右/左
被引用控件要在引用控件之前,否则会找不到
android:layout_alignRight / alignLeft / alignTop / alignBottom相对控件边缘 右/左

3.3.3 帧布局 FrameLayout
所有控件会默认放在左上角
3.3.4 百分比布局 PercentFrameLayout
app:layout_widthPercent / heightPercent (%)
android:layout_gravity = “location | loacation”

你可能感兴趣的:(Android)