後臺動態添加布局文件、控件與動態設置屬性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.csdn.net/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.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