zoukankan      html  css  js  c++  java
  • 57.动态添加子View(Java/XML两种方式)

       /**
         * 动态添加子View(Java)
         */
        private View createViewWithJava() {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout view = new LinearLayout(this);
            view.setLayoutParams(lp);//设置布局参数
            view.setOrientation(LinearLayout.HORIZONTAL);// 设置子View的Linearlayout// 为垂直方向布局

            //定义子View中两个元素的布局
            ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            ViewGroup.LayoutParams vlp2 = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

            TextView tv1 = new TextView(this);
            TextView tv2 = new TextView(this);
            tv1.setLayoutParams(vlp);//设置TextView的布局
            tv2.setLayoutParams(vlp2);
            tv1.setText("姓名:");
            tv2.setText("小明");
            tv2.setPadding(calculateDpToPx(50), 0, 0, 0);//设置边距
            view.addView(tv1);//将TextView 添加到子View 中
            view.addView(tv2);//将TextView 添加到子View 中
            return view;
        }

        /**
         * 动态添加子View(Xml)
         */
        private View createViewWithXml() {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.a1_post_params, null);//也可以从XML中加载布局

            view.setLayoutParams(lp);//设置布局参数

            EditText key = (EditText) view.findViewById(R.id.key);
            EditText value = (EditText) view.findViewById(R.id.value);

            //添加进键值集合
            keys.add(key);
            values.add(value);

            return view;
        }




  • 相关阅读:
    实用小软件
    没有找到MSVCP71.dll,迅雷5无法进行离线下载,P2P Seacher无法连入emule网络
    PSP2000V3版5.03系统误删PSP文件夹的拯救方案
    图书馆图书检索的小技巧
    thinkpad指点杆(trackpoint)在WPS的word文档中失效的解决办法
    笔记本电池死而复生
    调试Page.IsPostBack,感觉好奇怪
    OleDbSchemaGuid.Columns返回DataTable介绍
    静态类生命周期的问题
    IE中居中,FF中出问题
  • 原文地址:https://www.cnblogs.com/yutianran/p/5069651.html
Copyright © 2011-2022 走看看