標籤:代碼布局 動態代碼 代碼布局和xml布局
動態代碼布局
- 如何添加代碼布局
- 代碼布局注意的問題
- 代碼布局和XML布局的效能比較
如何添加代碼布局
for example —— 簡單布局LinearLayout
LinearLayout llayout = new LinearLayout(mContext); llayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT ); llayout.setLayoutParams(layoutParams); Button btn = new Button(mContext); btn.setText("This is Button"); btn.setPadding(8, 8, 8, 8); btn.setLayoutParams(lp); llayout.addView(btn); //這是在Activity的onCreate()中設定布局 setContentView(llayout); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "This is dynamic activity", Toast.LENGTH_LONG).show(); } });
another example —— 複雜布局RelativeLayout
痛點:子控制項的相對位置關係的處理
//父控制項 RelativeLayout myLayout = new RelativeLayout(this); myLayout.setBackgroundColor(Color.BLUE); //兩個子控制項 Button myButton = new Button(this); EditText myEditText = new EditText(this); //重點:產生對應的ID myButton.setId(generateViewId()); myEditText.setId(generateViewId()); //子控制項位置 RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL); buttonParams.addRule(RelativeLayout.CENTER_VERTICAL); RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); textParams.addRule(RelativeLayout.CENTER_HORIZONTAL); textParams.setMargins(0, 0, 0, 80); //重點在這裡 textParams.addRule(RelativeLayout.ABOVE, myButton.getId()); //添加布局 myLayout.addView(myButton, buttonParams); myLayout.addView(myEditText, textParams); setContentView(myLayout);
重點要說的是generateViewId(),這個可以專門放到工具類裡:
/** * An {@code int} value that may be updated atomically. */ private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1); /** * 動態產生View ID * API LEVEL 17 以上View.generateViewId()產生 * API LEVEL 17 以下需要手動產生 */ public static int generateViewId() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { for (; ; ) { final int result = sNextGeneratedId.get(); // aapt-generated IDs have the high byte nonzero; clamp to the range under that. int newValue = result + 1; if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0. if (sNextGeneratedId.compareAndSet(result, newValue)) { return result; } } } else { return View.generateViewId(); } }
代碼布局注意的問題
控制項不能重複使用
// 第一次添加
mLinearLayout.addView(mTextView, mLayoutParams);
// 第二次添加
mLinearLayout.addView(mTextView, mLayoutParams);
//我們重複添加了兩次mTextView。這個是不允許的,在父類布局中,只能有唯一的對象,不能重複。
不同Activity中 Id 相同是否會報錯
直接設定ID setId(1) 是不行的
產生ID須使用View.generateViewId()
我的意見是建立靜態工具類來產生ID
關於ID的int相同是否會出錯的問題,目前還沒有驗證
一些常用的代碼
textView.setTextColor(0xffff0000);
layout.setBackgroundColor(0x00000000);
setOrientation(LinearLayout.VERTICAL);
setGravity(Gravity.CENTER_VERTICAL)
setPadding(10, 5, 5, 5);
setMargin(8, 0, 0, 0);
lp.gravity = Gravity.CENTER_VERTICAL;
lp.topMargin = 5;
lp.addRule(RelativeLayout.CENTER_VERTICAL);
lp.addRule(RelativeLayout.RIGHT_OF, ID_IMAGE_HEAD);
代碼布局和XML布局的效能比較
測試一個簡單的例子
使用代碼布局如下:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = this; LinearLayout llayout = new LinearLayout(mContext); llayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT ); llayout.setLayoutParams(layoutParams); TextView tv = new TextView(mContext); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(8, 8, 8, 8); tv.setLayoutParams(lp); tv.setText("This is TextView"); tv.setPadding(8, 8, 8, 8); llayout.addView(tv); Button btn = new Button(mContext); btn.setText("This is Button"); btn.setPadding(8, 8, 8, 8); btn.setLayoutParams(lp); setContentView(llayout); }
三次測量平均值: (23+28 + 20)/3 = 23.67ms
使用相同XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:text="This is TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8px" android:padding="8px"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is Button" android:padding="8px"/></LinearLayout>
三次測量平均值: (20 + 26 + 24)/3 = 23.33ms
結論:儘管樣本單一,數量也很少,但是說明 代碼布局 和 XML布局 載入時間基本相同,效能基本相同。
不足:更複雜的介面,暫時未測試。
想法:代碼布局是必要的,如果總結代碼布局為模版和庫,利用泛型和反射的,在介面的複用和自動化上,將比XML更方便擴充。
Android原理——動態代碼布局