android 用代碼編寫linearlayout布局

來源:互聯網
上載者:User

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至此,簡單的手寫代碼編寫介面介紹完畢,其他複雜的介面都可依次類推!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.