




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
Android 用純代碼實現復雜界面在開發(fā)Android應用時有時會遇到純代碼實現復雜界面的需求,本文通過實例來演示,希望能對大家有所幫助界面截圖:XML布局文件: 通過純代碼實現XML同樣的效果: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 Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);/setContentView(R.);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;/初始化ImageViewImageView imageMain = new ImageView(this);imageMain.setScaleType(ScaleType.FIT_CENTER);imageMain.setAdjustViewBounds(true);imageMain.setBackgroundColor(Color.BLACK);imageMain.setImageResource(android.R.drawable.ic_dialog_map);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;/初始化textInfoTextView 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;/初始化editInfoEditText 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);/初始化ImageViewImageView imageBottom = new ImageView(this);imageBottom.setScaleType(ScaleType.FIT_CENTER);imageBottom.setAdjustViewBounds(true);imageBottom.setBackgroundColor(0xFF777777);imageBottom.setImageResource(android.R.drawable.ic_dialog_email);la
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 徐州工程學院《服裝與化妝技巧》2023-2024學年第一學期期末試卷
- 與但丁討論神曲高清大圖(含詳細人名)
- 2025-2030寵物罐頭市場發(fā)展現狀調查及供需格局分析研究報告
- 平安公務車系統(tǒng)培訓資料
- 金鷹國際花園整合行銷方案
- 2025-2030年中國AMT自動變速器行業(yè)市場供需現狀及投資決策建議研究
- 家園共育的實施與反思計劃
- 生物知識分享討論計劃
- 社團團隊建設活動的設計計劃
- 新興科技對品牌建設的影響計劃
- 2025陜西漢中漢源電力(集團)限公司招聘56人易考易錯模擬試題(共500題)試卷后附參考答案
- 南京師范大學自主招生個人陳述范文與撰寫要點
- 鐵粉運輸合同協(xié)議
- 計算機網絡安全知識試題及答案2025年計算機二級考試
- 浙江省A9協(xié)作體2024-2025學年高二下學期4月期中聯考語文試卷(含答案 )
- 2025年初中學業(yè)水平考試地理模擬卷及答案:圖表解讀與地理學科創(chuàng)新試題
- (四調)武漢市2025屆高中畢業(yè)生四月調研考試 語文試卷(含答案詳解)
- 第4單元 亮火蟲(教學設計)-2024-2025學年粵教花城版(2024)音樂一年級下冊
- 車間生產材料管理制度
- 西師大版小學五年級數學(下)期中測試題(含答案)
- 廣東省深圳市龍華區(qū)2023-2024學年七年級下學期期中數學試題(含答案)
評論
0/150
提交評論