后台动态添加布局文件、控件与动态设置属性



后台动态添加布局文件、控件与动态设置属性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.youkuaiyun.com/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.youkuaiyun.com/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.youkuaiyun.com/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.youkuaiyun.com/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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值