後臺動態添加布局文件、控件與動態設置屬性



後臺動態添加布局文件、控件與動態設置屬性2


翻譯佈局文件
佈局文件
[html] view plain copy
  1. <LinearLayout      
  2.         android:layout_width="fill_parent"      
  3.         android:background="@drawable/myborder"  
  4.         android:layout_marginTop="15dp"  
  5.         android:orientation="vertical"  
  6.         android:clickable="true"  
  7.         android:layout_height="wrap_content">      
  8.         <RelativeLayout      
  9.             android:layout_width="fill_parent"      
  10.             android:layout_height="39dp"       
  11.             android:gravity="center_horizontal">      
  12.           
  13.             <ImageView      
  14.                 android:id="@+id/iv_system_left"      
  15.                 android:layout_width="wrap_content"      
  16.                 android:layout_height="wrap_content"      
  17.                 android:layout_alignParentLeft="true"      
  18.                 android:layout_centerVertical="true"      
  19.                 android:layout_marginLeft="12dp"     
  20.                 android:src="@drawable/set7" />    
  21.                   
  22.             <TextView      
  23.                 android:id="@+id/tv_system_title"      
  24.                 android:layout_width="wrap_content"      
  25.                 android:layout_height="wrap_content"    
  26.                 android:layout_toRightOf="@+id/iv_system_left"      
  27.                 android:layout_centerVertical="true"      
  28.                 android:layout_marginLeft="11dp"      
  29.                 android:text="分享"       
  30.                 android:textColor="#000000"/>      
  31.           
  32.             <ImageView      
  33.                 android:id="@+id/iv_system_right"      
  34.                 android:layout_width="wrap_content"      
  35.                 android:layout_height="wrap_content"      
  36.                 android:layout_alignParentRight="true"      
  37.                 android:layout_centerVertical="true"      
  38.                 android:layout_marginRight="12dp"      
  39.                 android:src="@drawable/ios_arrow" />      
  40.         </RelativeLayout>      
  41.           
  42.             <LinearLayout android:layout_width="fill_parent"  
  43.         android:layout_height="1px"  
  44.         android:background="#cccccc"   
  45.         android:layout_marginLeft="46dp"  
  46.         ></LinearLayout>  
  47.     </LinearLayout>     
後臺生成
[java] view plain copy
  1. public LinearLayout CreateSetting(Drawable img,String _text)  
  2.     {  
  3.         RelativeLayout  rl = new RelativeLayout(getActivity().getApplicationContext());  
  4.         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(  
  5.                 RelativeLayout.LayoutParams.MATCH_PARENT,Dp2Px(getActivity(),39));  
  6.          lp.addRule(RelativeLayout.CENTER_HORIZONTAL);  
  7.          rl.setLayoutParams(lp);  
  8.                    
  9.          RelativeLayout.LayoutParams lpiv = new RelativeLayout.LayoutParams(  
  10.                     RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
  11.          lpiv.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);  
  12.          lpiv.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);  
  13.          lpiv.setMargins(Dp2Px(getActivity(),12), 000);  
  14.          //lpiv.addRule(RelativeLayout.RIGHT_OF,iv.getId());  
  15.            
  16.          ImageView iv = new ImageView(getActivity().getApplicationContext());  
  17.          iv.setLayoutParams(lpiv);  
  18.          iv.setImageDrawable(img);  
  19.            
  20.          iv.setId(99);//動態相對佈局必須設置id,getid才能取到  
  21.            
  22.          RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(  
  23.                     RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
  24.          lp_tv.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);  
  25.          lp_tv.setMargins(Dp2Px(getActivity(),11), 000);  
  26.          lp_tv.addRule(RelativeLayout.RIGHT_OF,iv.getId());  
  27.          TextView tv = new TextView(getActivity().getApplicationContext());  
  28.          tv.setLayoutParams(lp_tv);  
  29.          tv.setText(_text);  
  30.          tv.setTextColor(Color.parseColor("#000000"));  
  31.            
  32.            
  33.          RelativeLayout.LayoutParams lp_img = new RelativeLayout.LayoutParams(  
  34.                     RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
  35.          lp_img.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);  
  36.          lp_img.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);  
  37.          lp_img.setMargins(00, Dp2Px(getActivity(),12), 0);  
  38.          //lpiv.addRule(RelativeLayout.RIGHT_OF,iv.getId());  
  39.            
  40.          ImageView ivarrow = new ImageView(getActivity().getApplicationContext());  
  41.          ivarrow.setLayoutParams(lp_img);  
  42.          ivarrow.setImageDrawable(this.getResources().getDrawable(R.drawable.ios_arrow));  
  43.            
  44.          rl.addView(iv);  
  45.          rl.addView(tv);  
  46.          rl.addView(ivarrow);  
  47.            
  48.            
  49.          LinearLayout  ll_line = new LinearLayout(getActivity().getApplicationContext());  
  50.          LinearLayout.LayoutParams lp_line = new LinearLayout.LayoutParams(  
  51.                     RelativeLayout.LayoutParams.MATCH_PARENT,1);  
  52.          lp_line.setMargins(Dp2Px(getActivity(),46), 000);  
  53.          ll_line.setLayoutParams(lp_line);  
  54.          ll_line.setBackgroundColor(Color.parseColor("#cccccc"));  
  55.            
  56.            
  57.          LinearLayout  ll_f = new LinearLayout(getActivity().getApplicationContext());  
  58.          LinearLayout.LayoutParams lp_f = new LinearLayout.LayoutParams(  
  59.                     RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
  60.        
  61.          ll_f.setLayoutParams(lp_f);           
  62.          ll_f.setOrientation(LinearLayout.VERTICAL);  
  63.          ll_f.setClickable(true);  
  64.          Drawable ll_fd = this.getResources().getDrawable(R.drawable.myborder);  
  65.          try{  
  66.              ll_f.setBackgroundDrawable(ll_fd);//還必須要使用這個方法,雖然提示過期  
  67.          }  
  68.          catch(Exception e){  
  69.              ll_f.setBackgroundColor(Color.parseColor("#ffffff"));  
  70.          }  
  71.          ll_f.addView(rl);  
  72.          ll_f.addView(ll_line);        
  73.        return ll_f;  
  74.     }  
[java] view plain copy
  1. public int Dp2Px(Context context, float dp) {   
  2.         final float scale = context.getResources().getDisplayMetrics().density;   
  3.         return (int) (dp * scale + 0.5f);   
  4.     }  


後臺設置高寬
[java] view plain copy
  1. LayoutParams lp;          
  2.       lp=mbtn.getLayoutParams();  
  3.       lp.width=100;  
  4.       lp.height=200;          
  5.       mbtn.setLayoutParams(lp);  


 android:background="@drawable/layout_leftradiusborder"可以不用setBackgroundDrawable提示過期了,
可以使用 ll_f.setBackgroundResource(R.drawable.layout_leftradiusborder);


LinearLayout.LayoutParams.WRAP_CONTENT與
RelativeLayout.LayoutParams.WRAP_CONTENT感覺都是一樣的效果



一:在後臺添加表格
       
[java] view plain copy
  1. public void Eidt(List<TableItem> _tis,LinearLayout popView,Activity _mActivity)  
  2. {         
  3.        TableLayout tl = (TableLayout)popView.findViewById(R.id.mytab);  
  4.        tl.setStretchAllColumns(true);  
  5.          
  6.          
  7.        TableRow.LayoutParams trlpf = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,  
  8.             TableRow.LayoutParams.WRAP_CONTENT);//表格內添加的控件必須要用TableRow.LayoutParams不然調用tl.setStretchAllColumns會報錯  
  9.        trlpf.setMargins(0900);  
  10.        trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;//相當於layout_gravity  
  11.          
  12.        TableRow.LayoutParams trlpfe = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,  
  13.             TableRow.LayoutParams.WRAP_CONTENT);  
  14.        trlpfe.setMargins(0900);  
  15.          
  16.        for(int i=1;i<_tis.size()-4;i++)//  
  17.        {  
  18.         TableItemNameValueShrink tt = (TableItemNameValueShrink)_tis.get(i);  
  19.           
  20.            TableRow tr = new TableRow(_mActivity);  
  21.            tr.setLayoutParams(trlpf);  
  22.              
  23.              
  24.            TextView tv = new TextView(_mActivity);  
  25.            tv.setTextColor(Color.parseColor("#bbbbbb"));  
  26.            tv.setLayoutParams(trlpf);  
  27.            tv.setGravity(Gravity.RIGHT);//設置文字居中  
  28.            tv.setText(tt.name+":");  
  29.            tv.setEms(4);//小技巧文字不夠可以設置此固定幾個字符的長度  
  30.              
  31.            EditText ed = new EditText(_mActivity);  
  32.            ed.setLayoutParams(trlpfe);  
  33.            ed.setText(tt.value);  
  34.            ed.setTextColor(Color.parseColor("#666666"));  
  35.            ed.setEms(9);  
  36.                     
  37.            tr.addView(tv);  
  38.            tr.addView(ed);                                 
  39.         tl.addView(tr);           
  40.        }       
  41. }  

             
二:常用樣式設置


[java] view plain copy
  1.               TableRow.LayoutParams trlpf = new TableRow.LayoutParams(0,  
  2. TableRow.LayoutParams.WRAP_CONTENT,7);  
  3. trlpf.setMargins(2200); //left, top, right, bottom  
  4. trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;//設置layoutmagin  
  5.   
  6.   
  7.          TextView staName =  new TextView(WTRes.this);  
  8.  staName.setLayoutParams(trlpf);//添加布局  
  9.  staName.setText(jsonObj.getString("staName"));  
  10.  staName.setTextColor(Color.parseColor("#000000"));  
  11.  staName.setBackgroundColor(Color.parseColor("#BCD553"));  
  12.  staName.setTextSize(14);  
  13.  staName.setGravity(Gravity.CENTER);//設置文字居中  
  14.  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():


http://blog.csdn.net/feng88724/article/details/6333809


三:動態添加布局文件

[java] view plain copy
  1. LinearLayout layout;  
  2.     @Override  
  3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  4.             Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         layout=new LinearLayout(getActivity());  
  7.           ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(  
  8.                     ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);  
  9.         layout.setOrientation(LinearLayout.VERTICAL);//顯示方向  
  10.         layout.setLayoutParams(params);  
  11.                   
  12.         return layout;  
  13.         //return inflater.inflate(R.layout.mytable_trans, container, false);  
  14.         //return super.onCreateView(inflater, container, savedInstanceState);  
  15.     }  



四:動態相對佈局

[java] view plain copy
  1. TextView tv = new TextView(this);  
  2.         tv.setText("高度:100cm");  
  3.         tv.setTextColor(Color.BLUE);  
  4.           
  5.         RelativeLayout.LayoutParams tvparams = new RelativeLayout.LayoutParams(  
  6.                 ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);  
  7.         tvparams.setMargins(000,80);  
  8.         tvparams.addRule(RelativeLayout.CENTER_HORIZONTAL);//水平居中  
  9.         tvparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);//在佈局底部  
  10.         tv.setLayoutParams(tvparams); //使layout更新  
  11.           
  12.         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());

http://ask.csdn.net/questions/441

http://www.jb51.net/article/47566.htm


五: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);
}
http://blog.sina.com.cn/s/blog_4b20ae2e0101abvg.html
後臺動態添加布局文件、控件與動態設置屬性2


翻譯佈局文件
佈局文件
[html] view plain copy
  1. <LinearLayout      
  2.         android:layout_width="fill_parent"      
  3.         android:background="@drawable/myborder"  
  4.         android:layout_marginTop="15dp"  
  5.         android:orientation="vertical"  
  6.         android:clickable="true"  
  7.         android:layout_height="wrap_content">      
  8.         <RelativeLayout      
  9.             android:layout_width="fill_parent"      
  10.             android:layout_height="39dp"       
  11.             android:gravity="center_horizontal">      
  12.           
  13.             <ImageView      
  14.                 android:id="@+id/iv_system_left"      
  15.                 android:layout_width="wrap_content"      
  16.                 android:layout_height="wrap_content"      
  17.                 android:layout_alignParentLeft="true"      
  18.                 android:layout_centerVertical="true"      
  19.                 android:layout_marginLeft="12dp"     
  20.                 android:src="@drawable/set7" />    
  21.                   
  22.             <TextView      
  23.                 android:id="@+id/tv_system_title"      
  24.                 android:layout_width="wrap_content"      
  25.                 android:layout_height="wrap_content"    
  26.                 android:layout_toRightOf="@+id/iv_system_left"      
  27.                 android:layout_centerVertical="true"      
  28.                 android:layout_marginLeft="11dp"      
  29.                 android:text="分享"       
  30.                 android:textColor="#000000"/>      
  31.           
  32.             <ImageView      
  33.                 android:id="@+id/iv_system_right"      
  34.                 android:layout_width="wrap_content"      
  35.                 android:layout_height="wrap_content"      
  36.                 android:layout_alignParentRight="true"      
  37.                 android:layout_centerVertical="true"      
  38.                 android:layout_marginRight="12dp"      
  39.                 android:src="@drawable/ios_arrow" />      
  40.         </RelativeLayout>      
  41.           
  42.             <LinearLayout android:layout_width="fill_parent"  
  43.         android:layout_height="1px"  
  44.         android:background="#cccccc"   
  45.         android:layout_marginLeft="46dp"  
  46.         ></LinearLayout>  
  47.     </LinearLayout>     
後臺生成
[java] view plain copy
  1. public LinearLayout CreateSetting(Drawable img,String _text)  
  2.     {  
  3.         RelativeLayout  rl = new RelativeLayout(getActivity().getApplicationContext());  
  4.         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(  
  5.                 RelativeLayout.LayoutParams.MATCH_PARENT,Dp2Px(getActivity(),39));  
  6.          lp.addRule(RelativeLayout.CENTER_HORIZONTAL);  
  7.          rl.setLayoutParams(lp);  
  8.                    
  9.          RelativeLayout.LayoutParams lpiv = new RelativeLayout.LayoutParams(  
  10.                     RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
  11.          lpiv.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);  
  12.          lpiv.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);  
  13.          lpiv.setMargins(Dp2Px(getActivity(),12), 000);  
  14.          //lpiv.addRule(RelativeLayout.RIGHT_OF,iv.getId());  
  15.            
  16.          ImageView iv = new ImageView(getActivity().getApplicationContext());  
  17.          iv.setLayoutParams(lpiv);  
  18.          iv.setImageDrawable(img);  
  19.            
  20.          iv.setId(99);//動態相對佈局必須設置id,getid才能取到  
  21.            
  22.          RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(  
  23.                     RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
  24.          lp_tv.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);  
  25.          lp_tv.setMargins(Dp2Px(getActivity(),11), 000);  
  26.          lp_tv.addRule(RelativeLayout.RIGHT_OF,iv.getId());  
  27.          TextView tv = new TextView(getActivity().getApplicationContext());  
  28.          tv.setLayoutParams(lp_tv);  
  29.          tv.setText(_text);  
  30.          tv.setTextColor(Color.parseColor("#000000"));  
  31.            
  32.            
  33.          RelativeLayout.LayoutParams lp_img = new RelativeLayout.LayoutParams(  
  34.                     RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
  35.          lp_img.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);  
  36.          lp_img.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);  
  37.          lp_img.setMargins(00, Dp2Px(getActivity(),12), 0);  
  38.          //lpiv.addRule(RelativeLayout.RIGHT_OF,iv.getId());  
  39.            
  40.          ImageView ivarrow = new ImageView(getActivity().getApplicationContext());  
  41.          ivarrow.setLayoutParams(lp_img);  
  42.          ivarrow.setImageDrawable(this.getResources().getDrawable(R.drawable.ios_arrow));  
  43.            
  44.          rl.addView(iv);  
  45.          rl.addView(tv);  
  46.          rl.addView(ivarrow);  
  47.            
  48.            
  49.          LinearLayout  ll_line = new LinearLayout(getActivity().getApplicationContext());  
  50.          LinearLayout.LayoutParams lp_line = new LinearLayout.LayoutParams(  
  51.                     RelativeLayout.LayoutParams.MATCH_PARENT,1);  
  52.          lp_line.setMargins(Dp2Px(getActivity(),46), 000);  
  53.          ll_line.setLayoutParams(lp_line);  
  54.          ll_line.setBackgroundColor(Color.parseColor("#cccccc"));  
  55.            
  56.            
  57.          LinearLayout  ll_f = new LinearLayout(getActivity().getApplicationContext());  
  58.          LinearLayout.LayoutParams lp_f = new LinearLayout.LayoutParams(  
  59.                     RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
  60.        
  61.          ll_f.setLayoutParams(lp_f);           
  62.          ll_f.setOrientation(LinearLayout.VERTICAL);  
  63.          ll_f.setClickable(true);  
  64.          Drawable ll_fd = this.getResources().getDrawable(R.drawable.myborder);  
  65.          try{  
  66.              ll_f.setBackgroundDrawable(ll_fd);//還必須要使用這個方法,雖然提示過期  
  67.          }  
  68.          catch(Exception e){  
  69.              ll_f.setBackgroundColor(Color.parseColor("#ffffff"));  
  70.          }  
  71.          ll_f.addView(rl);  
  72.          ll_f.addView(ll_line);        
  73.        return ll_f;  
  74.     }  
[java] view plain copy
  1. public int Dp2Px(Context context, float dp) {   
  2.         final float scale = context.getResources().getDisplayMetrics().density;   
  3.         return (int) (dp * scale + 0.5f);   
  4.     }  


後臺設置高寬
[java] view plain copy
  1. LayoutParams lp;          
  2.       lp=mbtn.getLayoutParams();  
  3.       lp.width=100;  
  4.       lp.height=200;          
  5.       mbtn.setLayoutParams(lp);  


 android:background="@drawable/layout_leftradiusborder"可以不用setBackgroundDrawable提示過期了,
可以使用 ll_f.setBackgroundResource(R.drawable.layout_leftradiusborder);


LinearLayout.LayoutParams.WRAP_CONTENT與
RelativeLayout.LayoutParams.WRAP_CONTENT感覺都是一樣的效果



一:在後臺添加表格
       
[java] view plain copy
  1. public void Eidt(List<TableItem> _tis,LinearLayout popView,Activity _mActivity)  
  2. {         
  3.        TableLayout tl = (TableLayout)popView.findViewById(R.id.mytab);  
  4.        tl.setStretchAllColumns(true);  
  5.          
  6.          
  7.        TableRow.LayoutParams trlpf = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,  
  8.             TableRow.LayoutParams.WRAP_CONTENT);//表格內添加的控件必須要用TableRow.LayoutParams不然調用tl.setStretchAllColumns會報錯  
  9.        trlpf.setMargins(0900);  
  10.        trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;//相當於layout_gravity  
  11.          
  12.        TableRow.LayoutParams trlpfe = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,  
  13.             TableRow.LayoutParams.WRAP_CONTENT);  
  14.        trlpfe.setMargins(0900);  
  15.          
  16.        for(int i=1;i<_tis.size()-4;i++)//  
  17.        {  
  18.         TableItemNameValueShrink tt = (TableItemNameValueShrink)_tis.get(i);  
  19.           
  20.            TableRow tr = new TableRow(_mActivity);  
  21.            tr.setLayoutParams(trlpf);  
  22.              
  23.              
  24.            TextView tv = new TextView(_mActivity);  
  25.            tv.setTextColor(Color.parseColor("#bbbbbb"));  
  26.            tv.setLayoutParams(trlpf);  
  27.            tv.setGravity(Gravity.RIGHT);//設置文字居中  
  28.            tv.setText(tt.name+":");  
  29.            tv.setEms(4);//小技巧文字不夠可以設置此固定幾個字符的長度  
  30.              
  31.            EditText ed = new EditText(_mActivity);  
  32.            ed.setLayoutParams(trlpfe);  
  33.            ed.setText(tt.value);  
  34.            ed.setTextColor(Color.parseColor("#666666"));  
  35.            ed.setEms(9);  
  36.                     
  37.            tr.addView(tv);  
  38.            tr.addView(ed);                                 
  39.         tl.addView(tr);           
  40.        }       
  41. }  

             
二:常用樣式設置


[java] view plain copy
  1.               TableRow.LayoutParams trlpf = new TableRow.LayoutParams(0,  
  2. TableRow.LayoutParams.WRAP_CONTENT,7);  
  3. trlpf.setMargins(2200); //left, top, right, bottom  
  4. trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;//設置layoutmagin  
  5.   
  6.   
  7.          TextView staName =  new TextView(WTRes.this);  
  8.  staName.setLayoutParams(trlpf);//添加布局  
  9.  staName.setText(jsonObj.getString("staName"));  
  10.  staName.setTextColor(Color.parseColor("#000000"));  
  11.  staName.setBackgroundColor(Color.parseColor("#BCD553"));  
  12.  staName.setTextSize(14);  
  13.  staName.setGravity(Gravity.CENTER);//設置文字居中  
  14.  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():


http://blog.csdn.net/feng88724/article/details/6333809


三:動態添加布局文件

[java] view plain copy
  1. LinearLayout layout;  
  2.     @Override  
  3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  4.             Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         layout=new LinearLayout(getActivity());  
  7.           ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(  
  8.                     ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);  
  9.         layout.setOrientation(LinearLayout.VERTICAL);//顯示方向  
  10.         layout.setLayoutParams(params);  
  11.                   
  12.         return layout;  
  13.         //return inflater.inflate(R.layout.mytable_trans, container, false);  
  14.         //return super.onCreateView(inflater, container, savedInstanceState);  
  15.     }  



四:動態相對佈局

[java] view plain copy
  1. TextView tv = new TextView(this);  
  2.         tv.setText("高度:100cm");  
  3.         tv.setTextColor(Color.BLUE);  
  4.           
  5.         RelativeLayout.LayoutParams tvparams = new RelativeLayout.LayoutParams(  
  6.                 ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);  
  7.         tvparams.setMargins(000,80);  
  8.         tvparams.addRule(RelativeLayout.CENTER_HORIZONTAL);//水平居中  
  9.         tvparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);//在佈局底部  
  10.         tv.setLayoutParams(tvparams); //使layout更新  
  11.           
  12.         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());

http://ask.csdn.net/questions/441

http://www.jb51.net/article/47566.htm


五: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);
}
http://blog.sina.com.cn/s/blog_4b20ae2e0101abvg.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章