后台动态添加布局文件、控件与动态设置属性2
翻译布局文件
布局文件
- <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);
-
-
- ImageView iv = new ImageView(getActivity().getApplicationContext());
- iv.setLayoutParams(lpiv);
- iv.setImageDrawable(img);
-
- iv.setId(99);
-
- 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);
-
-
- 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;
- }
- public int Dp2Px(Context context, float dp) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dp * scale + 0.5f);
- }
后台设置高宽
- LayoutParams lp;
- lp=mbtn.getLayoutParams();
- lp.width=100;
- lp.height=200;
- mbtn.setLayoutParams(lp);
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);
- trlpf.setMargins(0, 9, 0, 0);
- trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;
-
- 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);
- trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;
-
-
- 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);
}
}
定义一个函数将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
翻译布局文件
布局文件
- <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);
-
-
- ImageView iv = new ImageView(getActivity().getApplicationContext());
- iv.setLayoutParams(lpiv);
- iv.setImageDrawable(img);
-
- iv.setId(99);
-
- 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);
-
-
- 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;
- }
- public int Dp2Px(Context context, float dp) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dp * scale + 0.5f);
- }
后台设置高宽
- LayoutParams lp;
- lp=mbtn.getLayoutParams();
- lp.width=100;
- lp.height=200;
- mbtn.setLayoutParams(lp);
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);
- trlpf.setMargins(0, 9, 0, 0);
- trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;
-
- 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);
- trlpf.gravity = Gravity.RIGHT|Gravity.CENTER;
-
-
- 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():
http://blog.youkuaiyun.com/feng88724/article/details/6333809
三:动态添加布局文件
- LinearLayout layout;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
-
- 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;
-
-
- }
四:动态相对布局
- 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.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