Android 用纯代码实现复杂界面

来源:互联网 发布:java实训学校 编辑:程序博客网 时间:2024/05/21 01:47

转自:http://blog.csdn.net/gf771115/article/details/8237229

在网上看到一个简单的用代码实现界面的例子,copy过来做参考。用代码实现界面的布局很多方法不能使用xml中的style,具体使用的方法需要边用边查。

-------------------------------------------------转载内容------------------------------------------------------------------

在开发Android应用时有时会遇到纯代码实现复杂界面的需求,本文通过实例来演示,希望能对大家有所帮助

界面截图:

 

XML布局文件:

<?xml version="1.0" encoding="utf-8"?><ScrollView     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@android:color/white">        <LinearLayout     android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:gravity="center">    <ImageView         android:layout_width="240dip"        android:layout_height="120dip"        android:layout_margin="30dip"        android:layout_gravity="center_horizontal"        android:background="@android:color/black"        android:scaleType="fitCenter"        android:adjustViewBounds="true"        android:src="@android:drawable/ic_dialog_map"/>    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_margin="30dip"        android:layout_gravity="center_horizontal"        android:gravity="center_horizontal"        android:textSize="18sp"        android:text="测试文本显示"/>    <EditText         android:layout_width="240dip"        android:layout_height="wrap_content"        android:layout_margin="30dip"        android:layout_gravity="center_horizontal"        android:hint="请输入文字内容"        android:maxLength="200"        android:textSize="18sp"/>    <LinearLayout         android:id="@+id/button_layout"        android:layout_width="240dip"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:background="#c6c3c6"        android:minHeight="54dip"        android:orientation="horizontal"        android:paddingTop="4dip"        android:paddingBottom="4dip"        android:paddingLeft="2dip"        android:paddingRight="2dip" >        <Button              android:text="确定 "            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="left"            android:layout_marginLeft="10dip"            android:layout_marginRight="5dip"            android:layout_weight="1"            android:maxLines="2"            android:textSize="18sp" />        <Button             android:text="取消"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="right"            android:layout_marginLeft="5dip"            android:layout_marginRight="10dip"            android:layout_weight="1"            android:maxLines="2"            android:textSize="18sp"/>    </LinearLayout>    <RelativeLayout         android:layout_width="fill_parent"        android:layout_height="wrap_content"        >        <ImageView             android:id="@+id/ImageBottom"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/button_layout"        android:layout_centerHorizontal="true"        android:layout_margin="30dip"        android:background="#FF777777"        android:scaleType="fitCenter"        android:adjustViewBounds="true"        android:src="@android:drawable/ic_dialog_email"/>    </RelativeLayout>    </LinearLayout>    </ScrollView>

通过纯代码实现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.layout.info);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);layoutBottom.addView(imageBottom, paramsImageBottom);layoutRoot.addView(layoutBottom);//TODO TEST//imageMain.setBackgroundResource(android.R.drawable.ic_dialog_map);textInfo.setText("测试文本显示");main.addView(layoutRoot);setContentView(main);}/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */public static int dip2px(Context context, float dpValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}/** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */public static int px2dip(Context context, float pxValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (pxValue / scale + 0.5f);}}


0 0