Android界面——控件和布局

常用控件

常用控件:TextView、Button、EditText、ImageView(图片)、ProgressBar(进度)、AlertDialog与ProgressDialog。
1.ProcessBar默认转圈圈,如果要设置为进度,在layout的ProgressBar里添加:

style="?android:attr/progressBarStyleHorizontal"
android:max="100"

另外,可以通过getVisibility()方法判断ProgressBar是否可见:View.VISIBLE(可见),View.INVISIBLE(不可见),View.GONE(隐藏)。invisible与gone的区别是gone不保留progress bar的空间,invisible保留。
2.AlertDialog与ProgressDialog。
效果都是弹出对话框,但是ProgressDialog对话框里有进度条,AlertDialog一般用做提示重要内容。
在这里插入图片描述
属性
gravity:指定文字的对齐方式,如top、bottom、left、right、center。gravity里可以用“|”指定多个值,如“center_vertical|center_horizontal”表示文字垂直居中+水平居中。
layout_gravity/:指定控件的对齐方式。
text、textColor、textAllCaps没啥好说的,字面意思。
textSize:指定文字大小,单位是“sp”。
hint:提示,android:hint=“按某某某格式输入。”
maxLines:规定最多有几行。
background:设定背景,android:background="@drawable/某某某.img"。

四种基本布局

线性布局、相对布局、帧布局、百分比布局(分为帧百分比布局和相对百分比布局)。
1.线性布局:水平线性排列(horizontal)、垂直线性排列(vertical)
2.相对布局:
(1)相对父类
android:layout_alignParentLeft=“true”
还有alignParentRight、alignParentTop、alignParentBottom、centerParent。
(2)相对某一控件
android:layout_above="@id/控件id",相对控件上面
android:layout_below="@id/控件id",相对控件下面
还有toLeftOf、toRightOf。
3.帧布局
布局不方便,默认所有东西都堆叠在左上角。
4.百分比布局——PercentFrameLayout、PercentRelativeLayout
线性布局有layout_weight,可以按照比例布局,但是帧布局和相对布局没有。百分比布局就相当于在原有布局的基础上扩展了百分比功能。
使用:(1)在"app/build.gradle"的dependence闭包里添加下面这一行,然后点击提示的“Sync Now”。

implementation 'com.android.support:percent:x.x.x' 

注意:新版本用implementation,旧版本用compile。x.x.x对应自己appcompat版本号(appcompat在dependencies里找)。
(2)在layout头部引用“xmlns:app",然后就可以在控件里用下面两行控制大小,用原有的android:layout_gravity控制摆放位置。

app:layout_widthPercent="xx%"
app:layout_heightPercent="x%"

自定义控件

引用布局
1.新建layout,设计、编辑布局
2.引用.。其它布局里:<include layout="@layout/某layout"/>
3.隐藏系统自带的标题栏

ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

自定义控件
编辑上面引用布局的逻辑,像普通的Activity一样,但是这个继承自引用布局的Layout。
通过LayoutInflater的from()方法构建一个LayoutInflater对象,然后通过inflate()动态加载。
注意:OnClickListener首字母大写。某人傻了吧唧的首字母小写,半天没补全。

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        LayoutInflater.from(context).inflate(R.layout.title,this);
        Button titleBack = (Button) findViewById(R.id.title_back);
        titleBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        });
    }
}

ListView、RecycleView下回分解。

猜你喜欢

转载自blog.csdn.net/spicyStrip/article/details/88359126