通常我們使用XML來書寫Android的圖形界面,XML雖然編寫方便,但是在某些情況下不如JAVA代碼靈活,所以在某些環境下還是必須使用JAVA代碼來編寫界面。我們以下XML代碼為例,編寫相應的JAVA代碼。
android:id="@+id/drawer_info"
android:layout_width="match_parent"
android:layout_height="120dp" >
android:id="@+id/user_head"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:src="@drawable/user_demo" >
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/user_head"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/user_head"
android:text="Mr Zdy"
android:textSize="18sp" >
android:id="@+id/user_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/user_name"
android:layout_marginLeft="20dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="@id/user_head"
android:text="zhudongya123@gmail.com"
android:textSize="14sp" >
上述XML代碼並不復雜。一個ImageView和兩個TextView。
最終效果圖是這個樣子的。相應的JAVA代碼如下。
我會在后面逐一解釋這些代碼。請忽略RelativeLayout背景和圓形的圖片,本例應該是普通的ImageView.
RelativeLayout rootlayout = new RelativeLayout(this);
rootlayout.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, dip2px(120)));
rootlayout.setId(0);
ImageView imageView = new ImageView(this);
RelativeLayout.LayoutParams image_Params = new RelativeLayout.LayoutParams(
dip2px(60), dip2px(60));
image_Params.setMargins(dip2px(20), dip2px(25), 0, dip2px(10));
imageView.setLayoutParams(image_Params);
imageView.setImageResource(R.drawable.user_demo);
imageView.setId(1);
TextView text_Name = new TextView(this);
RelativeLayout.LayoutParams text_Name_Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
text_Name_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
text_Name_Params.setMargins(dip2px(20), dip2px(10), 0, 0);
text_Name.setLayoutParams(text_Name_Params);
text_Name.setText("Mr.Zdy");
text_Name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
text_Name.setId(2);
TextView text_Email = new TextView(this);
RelativeLayout.LayoutParams text_Email_Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
text_Email_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
text_Email_Params.addRule(RelativeLayout.BELOW, text_Name.getId());
text_Email_Params.setMargins(dip2px(20), dip2px(3), 0, 0);
text_Email.setLayoutParams(text_Email_Params);
text_Email.setText("zhudongya123@gmail.com");
text_Email.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
rootlayout.addView(imageView, image_Params);
rootlayout.addView(text_Name, text_Name_Params);
rootlayout.addView(text_Email, text_Email_Params);
setContentView(rootlayout);
}
public int dip2px(float dpValue) {
final float scale = this.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
首先我們定義了一個RelativeLayout,起名為rootlayout。
這個是這個界面的根布局。
然后我們使用了setLayoutParams方法定義了該布局的長度和寬度。
在XML代碼中,我們是直接寫出View的width,height,margin。
而在Java代碼中,我們必須使用LayoutParams類來指定其屬性。
rootlayout.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, dip2px(120)));
RelativeLayout.LayoutParams繼承自ViewGroup.LayoutParams。
同時LinearLayout.LayoutParams也是繼承自ViewGroup.LayoutParams。
這些LayoutParams類用來在Java代碼中指定View的各種屬性。例如width,height,margin等等。
通常沒有特殊屬性時,我們可以直接使用LayoutParams。
例如:rootlayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
然后我們設置了rootlayout的ID.rootlayout.setId(0);
接下來我們定義了一個ImageView。
ImageView imageView = new ImageView(this);
RelativeLayout.LayoutParams image_Params = new RelativeLayout.LayoutParams(
dip2px(60), dip2px(60));
image_Params.setMargins(dip2px(20), dip2px(25), 0, dip2px(10));
imageView.setLayoutParams(image_Params);
imageView.setImageResource(R.drawable.user_demo);
imageView.setId(1);
在指定ImageView的寬度和高度時,我使用了一個方法dip2px,這個方法是將dp值轉化為px值,因為在XML中,我直接使用了120dp,60dp這樣的單位,但是在JAVA代碼中,默認單位是px(像素),所以我們必須定義了這樣一個方法來轉換單位。
public int dip2px(float dpValue) {
final float scale = this.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
同時我們使用了setMargins方法來指定margin屬性,使用了setImageResource方法來指定了相應的圖片資源。
使用setLayoutParams方法將定義好的屬性施加到ImageView上。
接下來是一個TextView。
TextView text_Name = new TextView(this);
RelativeLayout.LayoutParams text_Name_Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
text_Name_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
text_Name_Params.setMargins(dip2px(20), dip2px(10), 0, 0);
在定義RelativeLayout.LayoutParams變量text_Name_Params時,我使用了addRule這個方法。
addRule這里執行了兩次都是兩個參數,第一個代表屬性,第二個表示該與該屬性關聯的View的Id。
這里我們要注意,我們也可以這樣寫
text_Name_Params.addRule(RelativeLayout.RIGHT_OF, 1);
因為我們將ImageView的Id指定為1,但是當你使用imageView.getId()這樣的寫法時,你也必須在前面使用setId方法來指定其Id。
在某些情況下,addRule可能只有一個參數。
例如:addRule(RelativeLayout.CENTER_VERTICAL),有些時候第二個參數可能代表布爾值,例如addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
text_Name.setLayoutParams(text_Name_Params);
text_Name.setText("Mr.Zdy");
text_Name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
text_Name.setId(2);
使用setTextSize方法時,我們的第一個參數將單位強制指定為Sp,默認是Px。
最后我們使用addview方法將三個View加入rootLayout中。並使用setContentView加載視圖。
rootlayout.addView(imageView, image_Params);
rootlayout.addView(text_Name, text_Name_Params);
rootlayout.addView(text_Email, text_Email_Params);
setContentView(rootlayout);
addView也可以只有一個參數,將第二個Params參數省略,因為前面所有的View我都已經使用setLayoutParams指定其屬性,所以在這里addView方法也可不寫第二個參數。
個人觀點,僅供參考。
參考資料:
[addRule方法的使用](http://zhidao.baidu.com/link?url=R6E6Hwoea_SQFi5U_OwqZq0lqihpRkWYpOw9nZReYRc-1M4eUMzvyHjPn5FL0323gfSPssBLZLTgeKPeMrQ7W875uBxsUJbOaavpPmFCwMW)
[Android 步步為營 第5營 代碼控制UI,View](http://www.cnblogs.com/vivid-stanley/archive/2012/08/22/2651399.html)
[android-getTextSize返回值是以像素(px)為單位的,setTextSize()以sp為單位](http://blog.csdn.net/lizhenmingdirk/article/details/7349386)
[Android中LayoutParams的用法](http://www.cnblogs.com/zhengbeibei/archive/2013/03/29/2989263.html)