安卓表单数据的展示方式~!

最进公司再做一个办公类的软件,要把PC端的表单数据展示到手机上面!可以让你们看看,想象有多么恶心。

这里写图片描述

数据是一个object,不是数组,也不能弄个listview去一行行的展示。刚开始就是用XML布局去写代码,写好了20个左右的表单之后,需求变了(客户比较恶心),说要变样式,标题中间需要加一些空格,感觉要疯了一样

这里写图片描述

要做成这种样式,然后还要去修改布局,而且增删字段特别不方便,然后就想着不能这样做了,万一又改需求了,有人说可以用style统一样式啊,那样也可以,但是每个界面可能又30-40个字段需要去展示,又两行标题的有一行标题的,有可以编辑的,有列表样式的。每个界面都要去findViewById,然后去设置值,虽然可以用注解,但是维护起来很不方便,

我想到了手动去添加代码的方式,增删改都很方便,不用再去布局和代码里面去找了。

第一步

确定每个表单都有什么样式,然后就可以去自定义自己的样式了;我们这个表单的样式最多的一行标题,两行标题还有附件的样式,

我就挑最简单的样式给大家介绍一下,主要是这个思想:

首先创建一个single_title_view.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView

            style="@style/custum_title_style"
            android:id="@+id/single_item_tiele"
            android:layout_height="match_parent"
            android:paddingStart="5dp"
            android:paddingEnd="5dp"
            android:paddingBottom="7dp"
            android:paddingTop="8dp"
             />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/line_color"/>

        <TextView

            android:id="@+id/single_item_content"
            android:layout_width="0dp"
            android:textColor="@color/content_color1"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:paddingStart="5dp"
            android:paddingEnd="5dp"
            android:paddingBottom="7dp"
            android:paddingTop="8dp"/>




        <TextView

            android:visibility="gone"
            android:id="@+id/single_item_danwei"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:paddingStart="5dp"
            android:paddingEnd="5dp"
            android:paddingBottom="7dp"
            android:paddingTop="8dp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/line_color"/>
</LinearLayout>

下面来看自定义组合控件SingleTitleView

package com.demo.liebiao;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by lixuce on 2016/11/17.
 * 单行标题控件
 * setitem_title(Strins s)  设置标题
 * setitem_content(String s) 设置内容
 * setDanwei(String s)设置单位
 */

public class SingleTitleView extends LinearLayout {

    private TextView title_tv;
    private TextView content_tv;
    private TextView danwei_tv;

    public SingleTitleView(Context context) {
        super(context);

        initView(context);
    }



    private void initView(Context context) {
        View.inflate(context, R.layout.single_title_view, this);
        title_tv = ((TextView) findViewById(R.id.single_item_tiele));
        content_tv = ((TextView) findViewById(R.id.single_item_content));
        danwei_tv = ((TextView) findViewById(R.id.single_item_danwei));

    }
    /**
     * 设置标题
     * @param title  标题内容
     */
    public void setitem_title(String title) {
        if (isNotNull(title, title_tv)) {
            title_tv.setText(StringUtils.getformentString(title));
        }
    }
    /**
     * 设置内容
     * @param content  内容
     */
    public void setitem_content(String content) {
        if (isNotNull(content, content_tv)) {
            content_tv.setText(content);
        }
    }
    /**
     * 设置单位
     * @param danwei 单位内容
     */
    public void setDanwei(String danwei){
        if (isNotNull(danwei,danwei_tv)){
            danwei_tv.setVisibility(View.VISIBLE);
            danwei_tv.setText(danwei);
            if (">".equals(danwei)) {
                danwei_tv.setTextColor(Color.parseColor("#057dff"));
            }
        }
    }

    public void setContentColor(int s){
        content_tv.setTextColor(s);
    }

    /**
     * 判断控件和内容是否为空
     * @param s  内容
     * @param t   控件
     * @return    false==》空;true==》不为空
     */
    private boolean isNotNull(String s, TextView t) {
        return (s!=null&&!"".equals(s)&&!"null".equals(s)&&t!=null);
    }
}

组合控件里面主要封装了三个TextView 可以通过对应的方法去设置标题,内容,单位。

第二步

再需要展示表单的布局文件里面只需要去添加一个ScrollView–LinearLayout

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>
</ScrollView>

再代码里面通过findViewById去查找到linearLayout

 LinearLayout layout = (LinearLayout) view.findViewById(R.id.ll);

 SingleTitleView singleTitleView = new SingleTitleView(this);
        singleTitleView.setitem_title("我是表单的标题");
        singleTitleView.setitem_content("我是表单的内容");
        singleTitleView.setDanwei("元");

layout.addView(singleTitleView);

是不是很方便,一行代码就加载出来了,不用再去布局文件里面去findView了,activity类里面干净整齐,而且样式统一。如果客户再改需求什么,界面上面什么操作也不用做,直接去修改自定义的组合控件就可以,增删改都很方便,如果样式多的话可以创建一个工具类;

我只是提了一个最简单的例子,重要的这个是思想。

如果你要加载的详情界面的值是一个Object的话,不要再用listView去判断每行什么TYPE了,可以试试直接代码动态添加。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值