android如何对布局封装,《老罗Android第二季》布局、Dialog与Toast封装

1.  Android 目录介绍

1、src

编写java代码的地方

2、gen

系统的资源文件标识符,不需要程序员维护,系统自动生成

3、assets

原生资源文件,通常是方式html静态网页和视频声音资源文件。

4、bin

表示生成.apk的一个目录,可以直接按装在手机设备上

5、libs

表示使用第三方jar包。

6、res

1、drawable-XX

2、layout布局文件:布局设计器

3、menu菜单

4、values:放置手机用户界面的描述说明,包含文字等。国际化的使用等

7、AndroidManifest.xml清单文件

2. 线性布局

1.比重

只有线性布局才能设置比重。

通过 android:layout_weight="1"  设置比重。

2. 方向

如果 android:layout_width="0dp" 表示是水平方向定义的,android:layout_height="0dp"则表示是垂直定义的。

一般是设置android:weight属性的时候才使用,如果你的布局是按水平布局,设置控件比例,就设置android:layout_width=“0dp”然后就可以自己按照你所设置的比例进行显示,如果是竖直布局的话,设置控件比例,android:layout_height=“0dp”。

3. 布局性能优化

1. 布局重用方法一

如果有一布局要在多个地方使用,可以把它定义在一个 .xml 文件当中,然后用 标签添加进去。

layout="@layout/button"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:id="@+id/button"

/>

2. 布局重用方法二

如果定义了两个button,而这两个button在别的布局中也要用到,可以把它定义在一个单独的.xml文件当中。

这个文件在定义时,layout中选择 merge标签:

android:layout_weight="match_parent"

android:layout_height="match_parent">

android:id="@+id/login"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/login" />

android:id="@+id/reset"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/reset" />

使用merge可以减少 布局的层次。

使用的时候也是用 添加,同上面一样。

4. Dialog与Toast封装

封装一种自定义的AlertDialog 和 Toast.

public class CustomDialog {

private AlertDialog.Builder builder;

private Context context;

public CustomDialog(Context context) {

this.context = context;

}

public void createDialog(String buttontext, String title, String message,

final CallBack callBack) {

builder = new AlertDialog.Builder(context);

builder.setTitle(title);

builder.setMessage(message);

builder.setPositiveButton(buttontext, new OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {

callBack.isConfirm(true);

}

});

builder.create().show();

}

public interface CallBack {

public void isConfirm(boolean flag);

}

public void createToasts(String message,LayoutInflater layoutInflater) {

// Toast.makeText(context, message, Toast.LENGTH_LONG).show();

View view = layoutInflater.inflate(R.layout.toast, null);

TextView textView = (TextView)view.findViewById(R.id.text);

textView.setText(message);

Toast toast = new Toast(context);

toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(view);

toast.show();

}

}

这里定义了一个内部接口,接口定义了一个isConfirm方法,含有一个 flag 变量,用来记录是否按下 确定按钮。

在MainActivity中调用它们:

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {

CustomDialog dialog = new CustomDialog(MainActivity.this);

dialog.createDialog("确定", "提示", "您确定要删除吗?", new CallBack() {

public void isConfirm(boolean flag) {

System.out.println("----->>" + flag);

if (flag) {

//dosomething.....判断执行业务逻辑

}

}

});

}

});

button2.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {

CustomDialog dialog = new CustomDialog(MainActivity.this);

dialog.createToasts("网络有有异常!!",getLayoutInflater());

}

});

}

1.  Android的对话框有两种:PopupWindow和AlertDialog。

它们的不同点在于:AlertDialog的位置固定,而PopupWindow的位置可以随意

AlertDialog是非阻塞线程的,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。

而PopupWindow是阻塞线程的, 这就意味着在我们退出这个弹出框之前,程序会一直等待

PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下

showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移

showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移

showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

LayoutInflater layoutInflater = LayoutInflater.from(this);

View popupWindow = layoutInflater.inflate(R.layout.popup, null);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值