android后台动态添加布局文件、控件与动态设置属性 电脑版发表于:2014/10/27 17:25 **翻译布局文件** 布局文件 ``` <LinearLayout android:layout_width="fill_parent" android:background="@drawable/myborder" android:layout_marginTop="15dp" android:orientation="vertical" android:clickable="true" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="39dp" android:gravity="center_horizontal"> <ImageView android:id="@+id/iv_system_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="12dp" android:src="@drawable/set7" /> <TextView android:id="@+id/tv_system_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/iv_system_left" android:layout_centerVertical="true" android:layout_marginLeft="11dp" android:text="分享" android:textColor="#000000"/> <ImageView android:id="@+id/iv_system_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="12dp" android:src="@drawable/ios_arrow" /> </RelativeLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="1px" android:background="#cccccc" android:layout_marginLeft="46dp" ></LinearLayout> </LinearLayout> ``` **后台生成** ``` public LinearLayout CreateSetting(Drawable img,String _text) { RelativeLayout rl = new RelativeLayout(getActivity().getApplicationContext()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,Dp2Px(getActivity(),39)); lp.addRule(RelativeLayout.CENTER_HORIZONTAL); rl.setLayoutParams(lp); RelativeLayout.LayoutParams lpiv = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); lpiv.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE); lpiv.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE); lpiv.setMargins(Dp2Px(getActivity(),12), 0, 0, 0); //lpiv.addRule(RelativeLayout.RIGHT_OF,iv.getId()); ImageView iv = new ImageView(getActivity().getApplicationContext()); iv.setLayoutParams(lpiv); iv.setImageDrawable(img); iv.setId(99);//动态相对布局必须设置id,getid才能取到 RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); lp_tv.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE); lp_tv.setMargins(Dp2Px(getActivity(),11), 0, 0, 0); lp_tv.addRule(RelativeLayout.RIGHT_OF,iv.getId()); TextView tv = new TextView(getActivity().getApplicationContext()); tv.setLayoutParams(lp_tv); tv.setText(_text); tv.setTextColor(Color.parseColor("#000000")); RelativeLayout.LayoutParams lp_img = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); lp_img.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE); lp_img.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE); lp_img.setMargins(0, 0, Dp2Px(getActivity(),12), 0); //lpiv.addRule(RelativeLayout.RIGHT_OF,iv.getId()); ImageView ivarrow = new ImageView(getActivity().getApplicationContext()); ivarrow.setLayoutParams(lp_img); ivarrow.setImageDrawable(this.getResources().getDrawable(R.drawable.ios_arrow)); rl.addView(iv); rl.addView(tv); rl.addView(ivarrow); LinearLayout ll_line = new LinearLayout(getActivity().getApplicationContext()); LinearLayout.LayoutParams lp_line = new LinearLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,1); lp_line.setMargins(Dp2Px(getActivity(),46), 0, 0, 0); ll_line.setLayoutParams(lp_line); ll_line.setBackgroundColor(Color.parseColor("#cccccc")); LinearLayout ll_f = new LinearLayout(getActivity().getApplicationContext()); LinearLayout.LayoutParams lp_f = new LinearLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT); ll_f.setLayoutParams(lp_f); ll_f.setOrientation(LinearLayout.VERTICAL); ll_f.setClickable(true); Drawable ll_fd = this.getResources().getDrawable(R.drawable.myborder); try{ ll_f.setBackgroundDrawable(ll_fd);//还必须要使用这个方法,虽然提示过期 } catch(Exception e){ ll_f.setBackgroundColor(Color.parseColor("#ffffff")); } ll_f.addView(rl); ll_f.addView(ll_line); return ll_f; } ``` android:background="@drawable/layout_leftradiusborder"可以不用setBackgroundDrawable提示过期了, 可以使用 ll_f.setBackgroundResource(R.drawable.layout_leftradiusborder); LinearLayout.LayoutParams.WRAP_CONTENT与 RelativeLayout.LayoutParams.WRAP_CONTENT感觉都是一样的效果 **一:在后台添加表格** ``` public void Eidt(List<TableItem> _tis,LinearLayout popView,Activity _mActivity) { TableLayout tl = (TableLayout)popView.findViewById(R.id.mytab); tl.setStretchAllColumns(true); TableRow.LayoutParams trlpf = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);//表格内添加的控件必须要用TableRow.LayoutParams不然调用tl.setStretchAllColumns会报错 trlpf.setMargins(0, 9, 0, 0); trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;//相当于layout_gravity TableRow.LayoutParams trlpfe = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); trlpfe.setMargins(0, 9, 0, 0); for(int i=1;i<_tis.size()-4;i++)// { TableItemNameValueShrink tt = (TableItemNameValueShrink)_tis.get(i); TableRow tr = new TableRow(_mActivity); tr.setLayoutParams(trlpf); TextView tv = new TextView(_mActivity); tv.setTextColor(Color.parseColor("#bbbbbb")); tv.setLayoutParams(trlpf); tv.setGravity(Gravity.RIGHT);//设置文字居中 tv.setText(tt.name+":"); tv.setEms(4);//小技巧文字不够可以设置此固定几个字符的长度 EditText ed = new EditText(_mActivity); ed.setLayoutParams(trlpfe); ed.setText(tt.value); ed.setTextColor(Color.parseColor("#666666")); ed.setEms(9); tr.addView(tv); tr.addView(ed); tl.addView(tr); } } ``` 二:常用样式设置 ``` TableRow.LayoutParams trlpf = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT,7); trlpf.setMargins(2, 2, 0, 0); //left, top, right, bottom trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;//设置layoutmagin TextView staName = new TextView(WTRes.this); staName.setLayoutParams(trlpf);//添加布局 staName.setText(jsonObj.getString("staName")); staName.setTextColor(Color.parseColor("#000000")); staName.setBackgroundColor(Color.parseColor("#BCD553")); staName.setTextSize(14); staName.setGravity(Gravity.CENTER);//设置文字居中 tr.addView(staName); ``` ``` package yu.activity; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; public class TextViewJava extends Activity { private LinearLayout mLayout; private TextView mTextView; private RelativeLayout mLayout2; private TextView mTextView2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 创建一个线性布局 mLayout = new LinearLayout(this); // 接着创建一个TextView mTextView = new TextView(this); // 第一个参数为宽的设置,第二个参数为高的设置。 mTextView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); // 设置mTextView的文字 mTextView.setText("这是我的TextView"); // 设置字体大小 mTextView.setTextSize(20); // 设置背景 mTextView.setBackgroundColor(Color.BLUE); // 设置字体颜色 mTextView.setTextColor(Color.RED); //设置居中 mTextView.setGravity(Gravity.CENTER); // mTextView.setPadding(1, 0, 0, 0);//left, top, right, bottom // 将TextView添加到Linearlayout中去 mLayout.addView(mTextView); // 创建一个线性布局 mLayout2 = new RelativeLayout(this); // 接着创建一个TextView mTextView2 = new TextView(this); // 第一个参数为宽的设置,第二个参数为高的设置。 mTextView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); // 设置mTextView的文字 mTextView2.setText("这是我的TextView"); // 设置字体大小 mTextView2.setTextSize(20); // 设置背景 mTextView2.setBackgroundColor(Color.BLUE); // 设置字体颜色 mTextView2.setTextColor(Color.RED); // 设置居中 mTextView2.setGravity(Gravity.CENTER); //相对位置 RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mTextView2.getLayoutParams(); params.setMargins(1, 0, 0, 0);// 通过自定义坐标来放置你的控件left, top, right, bottom mTextView .setLayoutParams(params);// // 将TextView添加到RelativeLayout中去 mLayout2.addView(mTextView2); // 展现这个线性布局 setContentView(mLayout); setContentView(mLayout2); } } ``` 设置可见性 setVisibility(): **三:动态添加布局文件** ``` LinearLayout layout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub layout=new LinearLayout(getActivity()); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT); layout.setOrientation(LinearLayout.VERTICAL);//显示方向 layout.setLayoutParams(params); return layout; //return inflater.inflate(R.layout.mytable_trans, container, false); //return super.onCreateView(inflater, container, savedInstanceState); } ``` **四:动态相对布局** ``` TextView tv = new TextView(this); tv.setText("高度:100cm"); tv.setTextColor(Color.BLUE); RelativeLayout.LayoutParams tvparams = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); tvparams.setMargins(0, 0, 0,80); tvparams.addRule(RelativeLayout.CENTER_HORIZONTAL);//水平居中 tvparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);//在布局底部 tv.setLayoutParams(tvparams); //使layout更新 layout.addView(tv); ``` lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); // addRule方法 将按钮布局添加到button1的右边 params.addRule(RelativeLayout.RIGHT_OF, R.id.button1); lpiv.addRule(RelativeLayout.RIGHT_OF,iv.getId()); **五:listview后台设置高度,注意dp像素的转化** 定义一个函数将dp转换为像素 ``` public int Dp2Px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); } ``` 定义函数动态控制listView的高度 ``` public void setListViewHeightBasedOnChildren(ListView listView) { //获取listview的适配器 ListAdapter listAdapter = listView.getAdapter(); //item的高度 int itemHeight = 46; if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { totalHeight += Dp2Px(getApplicationContext(),itemHeight)+listView.getDividerHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight; listView.setLayoutParams(params); } ```