首页 > 代码库 > Android布局琐碎(原)

Android布局琐碎(原)

一、文本编辑框字符数限制问题

方法一:

在 xml 文件中设置文本编辑框属性作字符数限制

如:android:maxLength="10" 即限制最大输入字符个数为10

方法二:

在代码中使用InputFilter 进行过滤

//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20

二、margin与padding

Android的Margin和Padding跟Html的是一样的。如下图所示:黄色部分为Padding,灰色部分为Margin。

通俗的理解 Padding 为内边框,Margin 为外边框

三、Android给listview设置分割线

android:dividerHeight=""

android:divider=""

四、listview包含不同布局item

ListView包含不同Item的布局

  1)重写 getViewTypeCount() – 该方法返回多少个不同的布局

  2)重写 getItemViewType(int) – 根据position返回相应的Item

  3)根据view item的类型,在getView中创建正确的convertView

部分代码如下:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = (Integer) list.get(position).get("type");
        viewHolder1 holder1 = null;
        viewHolder2 holder2 = null;
        switch (type) {
        case TYPE_1:
            convertView = inflater.inflate(R.layout.add_customer_item2, parent,
                    false);
            holder1 = new viewHolder1();
            holder1.textView1 = (TextView) convertView
                    .findViewById(R.id.company_name);
            holder1.textView2 = (TextView) convertView
                    .findViewById(R.id.customer_coder);
            holder1.editText1 = (EditText) convertView
                    .findViewById(R.id.company_name_value);
            String editText1Str = (String) list.get(position).get(
                    "company_name_value");
            holder1.editText1.setText(editText1Str);
            holder1.editText2 = (EditText) convertView
                    .findViewById(R.id.customer_coder_value);
            String editText2Str = (String) list.get(position).get(
                    "customer_coder_value");
            holder1.editText2.setText(editText2Str);
            convertView.setTag(holder1);
            break;
        case TYPE_2:
            convertView = inflater.inflate(R.layout.add_customer_item3, parent,
                    false);
            holder2 = new viewHolder2();
            holder2.textView1 = (TextView) convertView
                    .findViewById(R.id.contact_name);
            holder2.textView2 = (TextView) convertView
                    .findViewById(R.id.contact_tel);
            holder2.textView3 = (TextView) convertView
                    .findViewById(R.id.contact_adress);
            holder2.editText1 = (EditText) convertView
                    .findViewById(R.id.contact_name_value);
            String editHolder2Text1Str = (String) list.get(position).get(
                    "contact_name_value");
            holder2.editText1.setText(editHolder2Text1Str);
            holder2.editText2 = (EditText) convertView
                    .findViewById(R.id.contact_tel_value);
            String editHolder2Text2Str = (String) list.get(position).get(
                    "contact_tel_value");
            holder2.editText2.setText(editHolder2Text2Str);
            holder2.editText3 = (EditText) convertView
                    .findViewById(R.id.contact_address_value);
            String editHolderText3Str = (String) list.get(position).get(
                    "contact_address_value");
            holder2.editText3.setText(editHolderText3Str);
            convertView.setTag(holder2);
            break;
        }
        return convertView;
    }