Android中如何动态的添加布局

1.场景还原

     在移动端实现用户与管理员意见交互的界面时,我困惑了半响,为什么?用户意见当然首先recyclerview,那么管理员反馈意见列表也用recyclerview?不,我的第一想法的确是那样的,不过戛然而止,因为如果两种角色的意见列表都有recyclerview的话,那就必须在用户意见的adapter里面嵌套管理员的recyclerview,先不说能不能实现,单单就一个界面两个recyclerview就卡起来了,所以这里我决定用户意见列表用recyclerview,而管理员意见列表用动态的添加布局来实现。

问题解决的截图:

Android中如何动态的添加布局_第1张图片

2.解决方法

①意见交互的xml

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="horizontal">
        <com.spotcheck.android.widget.CircleImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/iv_person"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:src="@drawable/head_1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:id="@+id/tv_feedback"
            android:padding="13dp"
            android:textSize="16sp"
            android:text="什么时候会有苹果版啊!快点出的副书记的恢复快结婚了啥都好疯狂就爱上打开房间号了快捷方式来贷款纠纷了老大说减肥"/>

    LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:id="@+id/v_link"
        android:layout_marginLeft="10dp"
        android:background="#F5F5F5">View>

    <LinearLayout
        android:id="@+id/ll_system_feedback"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">

    LinearLayout>


LinearLayout>
②adapter

private class FeedBackAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return null==feedList?0:feedList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if(convertView==null){
            holder=new MyViewHolder();
            convertView=View.inflate(FeedbackActivity.this,R.layout.item_feedback,null);
            holder.tv_feedback= (TextView) convertView.findViewById(R.id.tv_feedback);
           // holder.tv_system= (TextView) convertView.findViewById(R.id.tv_system_back);
            holder.ll_system= (LinearLayout) convertView.findViewById(R.id.ll_system_feedback);
            holder.iv_person= (CircleImageView) convertView.findViewById(R.id.iv_person);
            holder.v_link= (View) convertView.findViewById(R.id.v_link);
            convertView.setTag(holder);
        }

        holder= (MyViewHolder) convertView.getTag();
        holder.tv_feedback.setText(feedList.get(position).getContent());
        String ivPath= (String) SpUtils.getData(FeedbackActivity.this, SpConstants.HEADPIC,"");
        //Glide加载圆形图片
        final MyViewHolder finalHolder = holder;
        Glide.with(FeedbackActivity.this).load((String)SpUtils.getData(App.getAppCtx(), SpConstants.HEADPIC,""))
                .asBitmap()
                .error(R.drawable.head)
                .placeholder(R.drawable.head)
                .centerCrop().into(new BitmapImageViewTarget(finalHolder.iv_person) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(FeedbackActivity.this.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                finalHolder.iv_person.setImageDrawable(circularBitmapDrawable);
            }
        });

        if(null==feedList.get(position).getAnswer()||feedList.get(position).getAnswer().size()==0){
            holder.ll_system.setVisibility(View.GONE);
            holder.v_link.setVisibility(View.GONE);
        }else{
            holder.ll_system.setVisibility(View.VISIBLE);
            holder.v_link.setVisibility(View.VISIBLE);
            int admSize = feedList.get(position).getAnswer().size();
            for (int i=0;i<admSize;i++){
           /* String text= "管理员:"+feedList.get(position).getAnswer().get(i).getContent();
            SpannableStringBuilder style=new SpannableStringBuilder(text);
            // style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置textview的背景颜色
            style.setSpan(new ForegroundColorSpan(Color.parseColor("#007aff")),0,4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置文字的颜色
            holder.tv_system.setText(style);*/
                //先清空,再添加
                holder.ll_system.removeAllViews();
                addLinearLayout( holder.ll_system, (ArrayList<FeedbackInfo.ResultBean.ListBean.AnswerBean>) feedList.get(position).getAnswer());
            }
        }
        return convertView;
    }
}
③动态添加布局的代码:

private void addLinearLayout(LinearLayout syslayout, ArrayList<FeedbackInfo.ResultBean.ListBean.AnswerBean> initMissionList) {

    //initMissionList:存储几条测试数据
    for (int i = 0; i < initMissionList.size(); i++) {
        //LinearLayout默认是水平(0)居中,现在改为垂直居中
        syslayout.setOrientation(LinearLayout.VERTICAL);
        //实例化一个LinearLayout
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        //设置LinearLayout属性(宽和高)
        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置边距
        layoutParams.setMargins(0, 0, 0, 0);
        //将以上的属性赋给LinearLayout
        linearLayout.setLayoutParams(layoutParams);
        //管理员头像
        CircleImageView imageView = new CircleImageView(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.gravity =  Gravity.CENTER_VERTICAL;
        lp.setMargins(20,0,0,0);
        // LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(50, 50);
        imageView.setLayoutParams(lp);
        imageView.setImageResource(R.drawable.head_pm_big);
        //实例化一个TextView
        TextView tv = new TextView(this);
        //设置宽高以及权重
        LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置textview垂直居中
        tvParams.gravity = Gravity.CENTER_VERTICAL;
        tv.setLayoutParams(tvParams);
        tv.setTextSize(16);
        tv.setPadding(13,13,13,13);
       tv.setTextColor(getResources().getColor(R.color.black));
       // String systext = initMissionList.get(i).getContent();
        String systext= "管理员:"+initMissionList.get(i).getContent();
        SpannableStringBuilder style=new SpannableStringBuilder(systext);
        // style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置textview的背景颜色
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#007aff")),0,4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置文字的颜色
        tv.setText(style);

        View view = new View(this);
        LinearLayout.LayoutParams viewParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 5);
        view.setLayoutParams(viewParams);
        view.setBackgroundResource(R.color.bg_activity);
        linearLayout.addView(imageView);
        linearLayout.addView(tv);
        syslayout.addView(linearLayout);
        syslayout.addView(view);
    }

}
动态添加布局其实跟静态xml书写相差不大,好好琢磨一下,会有不一样的收获。

你可能感兴趣的:(android)