m_LinearLayout = new LinearLayout(this);//建立LinearLayout布局對象 m_LinearLayout.setOrientation(LinearLayout.VERTICAL);//設定布局LinearLayout的屬性 m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK); /*建立ListView對象*/ m_ListView = new ListView(this); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout. LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); m_ListView.setBackgroundColor(Color.BLACK); /*添加m_ListView到m_LinearLayout布局*/ m_LinearLayout.addView(m_ListView,param); setContentView(m_LinearLayout);//設定顯示m_LinearLayout布局
首先,建立線性布局對象 LinearLayout layout = new LinearLayout(this);//為本Activity建立一個線性布局對象//並且設定它的屬性 android:layout_width 與 android:layout_height 都為 FILL_PARENTLinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);然後,為本Activity建立一個TextView,代碼如下 TextView textView = new TextView(this);然後設定TextView的屬性textView.setText(R.string.hello);textView.setId(34);對於布局方面的屬性這樣來設定 LinearLayout.LayoutParams textviewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);接著線上性布局對象中加入這個TextViewlayout.addView(textView,textviewParams);//加入的同時,也就設定了TextView相對於布局對象的布局屬性 android:layout_width 與 android:layout_height最後一步,設定本Activity的頂級介面為線性布局setContentView(layout,layoutParams); //同時也就設定了布局對象的android:layout_width 與 android:layout_height至此,簡單的手寫代碼編寫介面介紹完畢,其他複雜的介面都可依次類推!