其实写这个主要是为了给我自己看的。
表示先介绍一下自己对自己现在会的两种方式的看法:
全部代码生成:所有的创建控件,布局,与控件之间位置都是用代码写的。
代码量复杂,而且适配性不好(因为代码不支持dp,转的话更费事);
优点,可以在任何位置创建(原来写过相对手机屏幕绝对位置创建的,如果不对可以指出。)
部分: 所有布局,控件,位置都设计好,之后再需要的时候创建引用。
因为我才用到这个,所以缺点暂时不明。
下面将附上两种方式的代码,在我自己的手机上,效果一样(大致一样,部分是后加的),但是到了别的机型,第一种就炸了;
全代码写的
@SuppressLint("ResourceType") private void createItemDetail(LinearLayout llyt_add, View view, PDItem pdItem){ // llyt_add.removeAllViews(); //分割线 LinearLayout.LayoutParams layoutParams_partition=new LinearLayout.LayoutParams(1, DensityUtil.dip2px(mContext,20)); layoutParams_partition.setMargins(DensityUtil.dip2px(mContext,5),0,DensityUtil.dip2px(mContext,5),0); LinearLayout.LayoutParams layoutParams_txt_line = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1); layoutParams_txt_line.setMargins(0,DensityUtil.dip2px(mContext,5),0,DensityUtil.dip2px(mContext,5)); TextView txt_line = new TextView(llyt_add.getContext()); txt_line.setBackground(view.getResources().getDrawable(R.color.item_item_title_background)); llyt_add.addView(txt_line, layoutParams_txt_line); LinearLayout.LayoutParams layoutParams_rlyt=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(mContext,60)); RelativeLayout rlyt_item_detail=new RelativeLayout(view.getContext()); llyt_add.addView(rlyt_item_detail,layoutParams_rlyt); RelativeLayout.LayoutParams layoutParams_txt1=new RelativeLayout.LayoutParams(DensityUtil.dip2px(mContext,120), ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams_txt1.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE); layoutParams_txt1.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE); TextView txt_date=new TextView(rlyt_item_detail.getContext()); // txt_date.setText("01:21:21"); txt_date.setText(pdItem.getdDateStr()); txt_date.setTextColor(R.color.black); txt_date.setGravity(Gravity.CENTER); txt_date.setTextSize(TEXT_SIZE); txt_date.setId(0b1); rlyt_item_detail.addView(txt_date,layoutParams_txt1); RelativeLayout.LayoutParams layoutParams_txt2=new RelativeLayout.LayoutParams(DensityUtil.dip2px(mContext,100), ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams_txt2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,DensityUtil.dip2px(mContext,16)); layoutParams_txt2.addRule(RelativeLayout.CENTER_VERTICAL); layoutParams_txt2.rightMargin=DensityUtil.dip2px(mContext,30); TextView txt_capation=new TextView(rlyt_item_detail.getContext()); txt_capation.setText(""+pdItem.getProdMete()); txt_capation.setTextColor(R.color.black); txt_capation.setGravity(Gravity.RIGHT); txt_capation.setId(0b10); rlyt_item_detail.addView(txt_capation,layoutParams_txt2); RelativeLayout.LayoutParams layoutParams_llyt=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT); layoutParams_llyt.addRule(RelativeLayout.RIGHT_OF,txt_date.getId()); layoutParams_llyt.addRule(RelativeLayout.LEFT_OF,txt_capation.getId()); LinearLayout llyt=new LinearLayout(rlyt_item_detail.getContext()); llyt.setOrientation(LinearLayout.VERTICAL); // llyt.setBackgroundColor(R.color.hei); rlyt_item_detail.addView(llyt,layoutParams_llyt); LinearLayout.LayoutParams layoutParams_llyt1=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0,1); LinearLayout llyt_up=new LinearLayout(rlyt_item_detail.getContext()); llyt_up.setOrientation(LinearLayout.HORIZONTAL); llyt.addView(llyt_up,layoutParams_llyt1); LinearLayout llyt_down=new LinearLayout(rlyt_item_detail.getContext()); llyt_down.setOrientation(LinearLayout.HORIZONTAL); llyt_down.setGravity(Gravity.CENTER_VERTICAL); llyt.addView(llyt_down,layoutParams_llyt1); LinearLayout.LayoutParams layoutParams_txt_up=new LinearLayout.LayoutParams(0,ViewGroup.LayoutParams.MATCH_PARENT,1); TextView txt_project_name=new TextView(rlyt_item_detail.getContext()); txt_project_name.setText(pdItem.getProjName()); txt_project_name.setTextSize(TEXT_SIZE); txt_project_name.setTextColor(R.color.black); txt_project_name.setMaxLines(1); txt_project_name.setEllipsize(TextUtils.TruncateAt.END); llyt_up.addView(txt_project_name,layoutParams_txt_up); TextView txt_project_unit=new TextView(rlyt_item_detail.getContext()); txt_project_unit.setText(pdItem.getCustomer()); txt_project_unit.setTextSize(TEXT_SIZE); txt_project_unit.setTextColor(R.color.black); txt_project_unit.setMaxLines(1); txt_project_unit.setEllipsize(TextUtils.TruncateAt.END); llyt_up.addView(txt_project_unit,layoutParams_txt_up); LinearLayout.LayoutParams layoutParams_txt_down1=new LinearLayout.LayoutParams(DensityUtil.dip2px(mContext,140), ViewGroup.LayoutParams.MATCH_PARENT); TextView txt_part_item=getTextView(pdItem.getConsPos(),rlyt_item_detail); llyt_down.addView(txt_part_item,layoutParams_txt_down1); llyt_down.addView(getPartition(rlyt_item_detail),layoutParams_partition); LinearLayout.LayoutParams layoutParams_txt_down2=new LinearLayout.LayoutParams(DensityUtil.dip2px(mContext,80), ViewGroup.LayoutParams.MATCH_PARENT); TextView txt_grade_item=getTextView(pdItem.getBetLev(),rlyt_item_detail); llyt_down.addView(txt_grade_item,layoutParams_txt_down2); llyt_down.addView(getPartition(rlyt_item_detail),layoutParams_partition); TextView txt_number_item=getTextView(pdItem.getRecipe(),rlyt_item_detail); llyt_down.addView(txt_number_item,layoutParams_txt_down2); llyt_down.addView(getPartition(rlyt_item_detail),layoutParams_partition); TextView txt_way_item=getTextView(pdItem.getPour(),rlyt_item_detail); llyt_down.addView(txt_way_item,layoutParams_txt_down2); } private TextView getTextView(String s,RelativeLayout rlyt_item_detail){ TextView textView=new TextView(rlyt_item_detail.getContext()); textView.setText(s); textView.setGravity(Gravity.CENTER); textView.setTextSize(TEXT_SIZE-2); textView.setMaxLines(1); textView.setEllipsize(TextUtils.TruncateAt.END); return textView; } @SuppressLint("ResourceAsColor") private TextView getPartition(RelativeLayout rlyt_item_detail){ TextView txt_partition=new TextView(rlyt_item_detail.getContext()); txt_partition.setBackgroundColor(R.color.head); return txt_partition; }
各个作用看一下就可以搞明白了
第二种,一半一半的
布局文件
<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_vertical" android:orientation="horizontal" android:padding="10dp"> <TextView android:id="@+id/txt_time" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="14:29:20" android:textColor="@color/black" android:textSize="12sp" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/txt_project_name" style="@style/text_style2" android:maxWidth="140dp" android:text="------" /> <TextView android:id="@+id/txt_project_unit" style="@style/text_style2" android:layout_marginLeft="10dp" android:text="-----" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/txt_part_item" style="@style/text_style1" android:text="----aaaaa" android:maxWidth="90dp"/> <View style="@style/v_diver_style" /> <TextView android:id="@+id/txt_grade_item" style="@style/text_style1" android:text="----aaaaa" android:maxWidth="65dp" /> <View style="@style/v_diver_style" /> <TextView android:id="@+id/txt_number_item" style="@style/text_style1" android:text="PB-001aaaaa" android:maxWidth="60dp" /> <View style="@style/v_diver_style" /> <TextView android:id="@+id/txt_way_item" style="@style/text_style1" android:text="----" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/txt_prodMete" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="9" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <View style="@style/h_diver_style"/> </LinearLayout>
代码中的
private void createItemDetail(LinearLayout llyt_add,PDItem pdItem){ View view=LayoutInflater.from(mContext).inflate(R.layout.item_detail_layout, null, false); TextView txt_date=view.findViewById(R.id.txt_time); txt_date.setText(pdItem.getdDateStr()); // txt_date.setText("01:21:21"); TextView txtProdMete=view.findViewById(R.id.txt_prodMete); txtProdMete.setText(""+pdItem.getProdMete()); TextView txt_project_name=view.findViewById(R.id.txt_project_name); txt_project_name.setText(pdItem.getProjName()); TextView txt_project_unit=view.findViewById(R.id.txt_project_unit); txt_project_unit.setText(pdItem.getCustomer()); TextView txt_part_item=view.findViewById(R.id.txt_part_item); txt_part_item.setText(pdItem.getConsPos()); TextView txt_grade_item=view.findViewById(R.id.txt_grade_item); txt_grade_item.setText(pdItem.getBetLev()); TextView txt_number_item=view.findViewById(R.id.txt_number_item); txt_number_item.setText(pdItem.getRecipe()); TextView txt_way_item=view.findViewById(R.id.txt_way_item); txt_way_item.setText(pdItem.getPour()); llyt_add.addView(view); }这个相比上面的更简洁,也更易理解。适配性也更高。