




已閱讀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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 河南省鄭州市2024-2025學年(人教版2024)七年級英語下學期期末模擬練習C(含答案)
- (湖南高考卷)2022年湖南省普通高中學業(yè)水平選擇性考試高考物化生+政史地真題試卷及答案
- 2025年輔警招聘考試綜合提升練習題含答案詳解(輕巧奪冠)
- (2025)輔警招聘考試試題庫參考答案詳解
- 2022年2月鞍山市直機關遴選公務員面試真題附詳解
- 2022年2月銀川市直遴選面試真題帶題目詳解
- 2022年11月三亞市直機關遴選公務員面試真題附解析
- 2025年云南省交通運輸綜合行政執(zhí)法局文山支隊硯山大隊執(zhí)法輔助人員招聘(1人)筆試備考試題附答案詳解(黃金題型)
- 2024年甘肅陜煤集團韓城煤礦招聘筆試真題含答案詳解(輕巧奪冠)
- 2025年皖北煤電集團總醫(yī)院招聘護理筆試備考題庫附答案詳解(黃金題型)
- 2024-2025學年天津市八年級下期末數學模擬試卷(附答案解析)
- 2025年繼續(xù)教育公需科目網絡考試試題及答案
- 湖北省武漢市2025屆高三年級五月模擬訓練試題數學試題及答案(武漢五調)
- 2025年湖北省襄陽市襄州區(qū)中考數學二模試卷
- 2024年浙江省單獨考試招生文化考試語文試卷真題(含答案詳解)
- MOOC 地下鐵道-中南大學 中國大學慕課答案
- 江蘇省蘇州市昆山市2022-2023學年四年級下學期期末數學試題
- SIDEL吹瓶機原理ppt課件
- 公司解散清算專項法律服務工作方案
- 轉發(fā)省局《關于加強非煤礦山安全生產班組建設的指導意見》的通知
- 第四節(jié)SS4改型電力機車常見故障處理
評論
0/150
提交評論