GridView、showDialog

来源:互联网 发布:生鲜网络平台 编辑:程序博客网 时间:2024/06/05 23:51

GridView

    xml:    <GridView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:numColumns="3"  列数        android:horizontalSpacing="5dp" 单元格的水平间距        android:verticalSpacing="5dp"  单元格的垂直间距        android:stretchMode="columnWidth" > 拉伸方式    </GridView>

在java中自己创建视图时需要通过要添加的目标容器的LayoutParams来配置布局参数(宽高、边距等等)

iv = new ImageView(GridViewActivity.this);    iv.setScaleType(ScaleType.FIT_CENTER);    // 添加宽高    AbsListView.LayoutParams lp = new AbsListView.LayoutParams(                        LayoutParams.MATCH_PARENT, 200);    //运用布局参数    iv.setLayoutParams(lp);    iv.setBackgroundColor(0xff333333);

弹出框

使用Activity自带的showDialog方法来显示

    showDialog(100);  //参数为id    这个方法调用之后Activity会去找有没有该id的Dialog,没有则触发onCreateDialog方法    protected Dialog onCreateDialog(int id) {        //创建dialog        if(id ==100){            return create2BtnDialog();        }        return super.onCreateDialog(id);    }
填充创建Dialog的逻辑    //弹出框的构造器    AlertDialog.Builder b = new AlertDialog.Builder(this);    //组装弹出框标题    b.setTitle("两个按钮的弹出框");    //组装弹出框内容    b.setMessage(R.string.dialog_msg);    //设置一个正向按钮    b.setPositiveButton("确定", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(LearnDialogActivity.this, "确定按钮被点击", 2000).show();            }    });    b.setNegativeButton("取消", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(LearnDialogActivity.this, "取消按钮被点击", 2000).show();            }        });    //创建Dialog对象    Dialog dialog = b.create();

自己来控制显示和隐藏,可以直接调用Dialog的show方法显示,dismiss方法或者cancel方法隐藏

if(null == twoBtnDialog){    twoBtnDialog = create2BtnDialog();}//显示弹出框twoBtnDialog.show();隐藏dialog.dismiss(); -->可以通过dialog.setOnDismissListener(listener)监听到或者dialog.cancel(); -->用dialog.setOnCancelListener(listener)监听

列表框,可以通过builder对象的setItems设置列表

final String[] items = {"打开","复制","剪切","重命名","删除","发送到"};    b.setItems(items, new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                setTitle(items[which]);                //处理列表点击的逻辑,which表示列表的下标            }        });

针对字符串数组,可以在资源下通过定义

     <string-array name="m_array">           <item>打开</item>     每个item表示一个数组项        <item>复制</item>    </string-array>

java中获取数组资源

String[] ary = getResources().getStringArray(R.array.m_array);

Toast

Toast t = Toast.makeText(this, "我是一个提示,你只能看看", Toast.LENGTH_SHORT);    //设置显示位置(相对位置,x方向的偏移,y方向的偏移)    t.setGravity(Gravity.LEFT|Gravity.TOP, 50, 200);    t.show();

自定义Toast

View layout = getLayoutInflater().inflate(R.layout.toast_layout, null);     TextView tv = (TextView) layout.findViewById(R.id.toast_content);    tv.setText("ABC");    Toast t = new Toast(getApplicationContext()); //Context上下文对象-->环境    //应用环境(组件窗口 服务) 窗口环境(Activity)    //设置布局    t.setView(layout);    t.setGravity(Gravity.CENTER, 0, 0);    //设置事件    t.setDuration(Toast.LENGTH_LONG);    t.show();
0 0
原创粉丝点击