Android UI詳解之動態佈局
1.相對佈局RelativeLayout
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//定義一個相對佈局
RelativeLayout rl = new RelativeLayout(this);
Button bt = new Button(this);
bt.setText("MYbutton");
bt.setId(1);
//添加子元素
//rl.addView(bt);
//定義參數,這個制定樣式,也就是佈局空間的位置
RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
Params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
Params.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
//btn1位於父View的頂部,在父View中水平居中
rl.addView(bt,Params);
setContentView(rl);
}
2、定義一個線性佈局LinearLayout
一個LinearLayout 和 這個LinearLayout裏邊一個 TextView 的關係 TextView 就算LinearLayout的子視圖 child view .需要注意的是LayoutParams只是ViewGroup的一個內部類 這裏邊這個也就是ViewGroup裏邊這個LayoutParams類是 base class 基類 實際上每個不同的ViewGroup都有自己的LayoutParams子類 比如LinearLayout 也有自己的 LayoutParams 大家打開源碼看幾眼就知道了
//創建一個線性佈局
private LinearLayout mLayout;
mLayout = (LinearLayout) findViewById(R.id.layout);
//現在我要往mLayout裏邊添加一個TextView
//你可能會想直接在佈局文件裏邊配置不就行了 那是 但是這裏爲了說明問題我們用代碼實現
TextView textView = new TextView(Activity01.this);
textView.setText("Text View" );
//這裏請不要困惑,這裏是設置 這個textView的佈局 FILL_PARENT WRAP_CONTENT 和在xml文件裏邊設置是一樣的如
/**<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text View"/>*/
//在xml裏邊怎麼配置高寬大家都會的。
//第一個參數爲寬的設置,第二個參數爲高的設置。
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
1);
//調用addView()方法增加一個TextView到線性佈局中
mLayout.addView(textView, p);
上面代碼就是先加載已經存在的Layout文件,然後再用代碼動態的改變,這種很常見。
LayoutParams繼承於Android.View.ViewGroup.LayoutParams.
LayoutParams相當於一個Layout的信息包,它封裝了Layout的位置、高、寬等信息。假設在屏幕上一塊區域是由一個Layout佔領的,如果將一個View添加到一個Layout中,最好告訴Layout用戶期望的佈局方式,也就是將一個認可的layoutParams傳遞進去。
可以這樣去形容LayoutParams,在象棋的棋盤上,每個棋子都佔據一個位置,也就是每個棋子都有一個位置的信息,如這個棋子在4行4列,這裏的“4行4列”就是棋子的LayoutParams。
但LayoutParams類也只是簡單的描述了寬高,寬和高都可以設置成三種值:
1,一個確定的值;
2,FILL_PARENT,即填滿(和父容器一樣大小);
3,WRAP_CONTENT,即包裹住組件就好
Demo
XML代碼
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="240dip"
android:layout_height="120dip"
android:layout_margin="30dip"
android:layout_gravity="center_horizontal"
android:background="@android:color/black"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="30dip"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="18sp"
android:text="HelloWorld"/>
<EditText
android:layout_width="240dip"
android:layout_height="wrap_content"
android:layout_margin="30dip"
android:layout_gravity="center_horizontal"
android:hint="請輸入文字內容"
android:maxLength="200"
android:textSize="18sp"/>
<LinearLayout
android:id="@+id/button_layout"
android:layout_width="240dip"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#c6c3c6"
android:minHeight="54dip"
android:orientation="horizontal"
android:paddingTop="4dip"
android:paddingBottom="4dip"
android:paddingLeft="2dip"
android:paddingRight="2dip" >
<Button
android:text="確定 "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:layout_weight="1"
android:maxLines="2"
android:textSize="18sp" />
<Button
android:text="取消"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="5dip"
android:layout_marginRight="10dip"
android:layout_weight="1"
android:maxLines="2"
android:textSize="18sp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
等效的java代碼
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class ActivityInfo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// setContentView(R.layout.info);
initUI();
}
public final void initUI(){
ScrollView main = new ScrollView(this);
main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
main.setBackgroundColor(Color.WHITE);
//根佈局參數
LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
layoutParamsRoot.gravity = Gravity.CENTER;
//根佈局
LinearLayout layoutRoot = new LinearLayout(this);
layoutRoot.setLayoutParams(layoutParamsRoot);
layoutRoot.setOrientation(LinearLayout.VERTICAL);
//上邊距(dp值)
int topMargin = dip2px(this, 30);
//imageMain寬度(dp值)
int widthMain = dip2px(this, 240);
//imageMain高度(dp值)
int heightMain = dip2px(this, 120);
//imageMain佈局參數
LinearLayout.LayoutParams layoutParamsImageMain = new LinearLayout.LayoutParams(widthMain,heightMain);
layoutParamsImageMain.topMargin = topMargin;
layoutParamsImageMain.bottomMargin = topMargin;
layoutParamsImageMain.leftMargin = topMargin;
layoutParamsImageMain.rightMargin = topMargin;
layoutParamsImageMain.gravity=Gravity.CENTER_HORIZONTAL;
//初始化ImageView
ImageView imageMain = new ImageView(this);
imageMain.setScaleType(ScaleType.FIT_CENTER);
imageMain.setAdjustViewBounds(true);
imageMain.setBackgroundColor(Color.BLACK);
imageMain.setImageResource(android.R.drawable.ic_launcher);
layoutRoot.addView(imageMain, layoutParamsImageMain);
//textInfo佈局參數
LinearLayout.LayoutParams layoutParamsTextInfo = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
layoutParamsTextInfo.topMargin = topMargin;
layoutParamsTextInfo.bottomMargin = topMargin;
layoutParamsTextInfo.leftMargin = topMargin;
layoutParamsTextInfo.rightMargin = topMargin;
layoutParamsTextInfo.gravity=Gravity.CENTER_HORIZONTAL;
//初始化textInfo
TextView textInfo = new TextView(this);
textInfo.setGravity(Gravity.CENTER_HORIZONTAL);
textInfo.setTextSize(18);
layoutRoot.addView(textInfo, layoutParamsTextInfo);
//editInfo佈局參數
LinearLayout.LayoutParams layoutParamsEditInfo = new LinearLayout.LayoutParams(widthMain,LayoutParams.WRAP_CONTENT);
layoutParamsEditInfo.topMargin = topMargin;
layoutParamsEditInfo.gravity=Gravity.CENTER_HORIZONTAL;
//初始化editInfo
EditText editInfo = new EditText(this);
editInfo.setHint("請輸入文字內容");
//設置可輸入的最大長度
InputFilter[] filters = {new LengthFilter(200)};
editInfo.setFilters(filters);
editInfo.setTextSize(18);
layoutRoot.addView(editInfo, layoutParamsEditInfo);
//上邊距(dp值)
int minHeight = dip2px(this, 54);
//上padding(dp值)
int topPadding = dip2px(this, 4);
//左padding(dp值)
int leftPadding = dip2px(this, 2);
//按鈕佈局
LinearLayout layoutButton = new LinearLayout(this);
layoutButton.setLayoutParams(layoutParamsEditInfo);
layoutButton.setOrientation(LinearLayout.HORIZONTAL);
layoutButton.setBackgroundColor(Color.parseColor("#c6c3c6"));
layoutButton.setMinimumHeight(minHeight);
layoutButton.setPadding(leftPadding, topPadding, leftPadding, topPadding);
layoutButton.setId(100000001);
//buttonOK佈局參數
LinearLayout.LayoutParams layoutParamsButtonOK = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParamsButtonOK.gravity = Gravity.LEFT;
layoutParamsButtonOK.leftMargin = dip2px(this, 10);
layoutParamsButtonOK.rightMargin = dip2px(this, 5);
layoutParamsButtonOK.weight = 1;
//Button確定
Button buttonOK = new Button(this);
buttonOK.setLayoutParams(layoutParamsButtonOK);
buttonOK.setMaxLines(2);
buttonOK.setTextSize(18);
buttonOK.setText("確定");
layoutButton.addView(buttonOK);
//buttonCancel佈局參數
LinearLayout.LayoutParams layoutParamsButtonCancel = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParamsButtonCancel.gravity = Gravity.RIGHT;
layoutParamsButtonCancel.leftMargin = dip2px(this, 5);
layoutParamsButtonCancel.rightMargin = dip2px(this, 10);
layoutParamsButtonCancel.weight = 1;
//Button取消
Button buttonCancel = new Button(this);
buttonCancel.setLayoutParams(layoutParamsButtonCancel);
buttonCancel.setMaxLines(2);
buttonCancel.setTextSize(18);
buttonCancel.setText("取消");
layoutButton.addView(buttonCancel);
layoutRoot.addView(layoutButton, layoutParamsEditInfo);
//RelativeLayout佈局參數
LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
RelativeLayout layoutBottom = new RelativeLayout(this);
layoutBottom.setLayoutParams(layoutParamsBottom);
RelativeLayout.LayoutParams paramsImageBottom = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsImageBottom.addRule(RelativeLayout.BELOW, 100000001);
paramsImageBottom.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
paramsImageBottom.setMargins(topMargin, topMargin, topMargin, topMargin);
//初始化ImageView
ImageView imageBottom = new ImageView(this);
imageBottom.setScaleType(ScaleType.FIT_CENTER);
imageBottom.setAdjustViewBounds(true);
imageBottom.setBackgroundColor(0xFF777777);
imageBottom.setImageResource(android.R.drawable.ic_dialog_email);
layoutBottom.addView(imageBottom, paramsImageBottom);
layoutRoot.addView(layoutBottom);
//TODO TEST
// imageMain.setBackgroundResource(android.R.drawable.ic_dialog_map);
textInfo.setText("測試文本顯示");
main.addView(layoutRoot);
setContentView(main);
}
/**
* 根據手機的分辨率從 dp 的單位 轉成爲 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根據手機的分辨率從 px(像素) 的單位 轉成爲 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}