android 純代碼 詳細編寫佈局文件

一,純代碼

佈局的引用。純代碼,聽着很刺激哦,android還有這個操作,牛逼吧,Google 66666

純代碼實現登錄界面,用一變二,看代碼,複雜的界面都是由簡單的演變而來,加油吧,騷年

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import song.com.cn.R;

/**
 * @author lixiang
 * @date :2017/11/3
 * @Description: 純代碼
 */

public class PureCodeActivity extends Activity {

    private EditText editInfo;
    private EditText passWord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initUI();
    }

    @SuppressLint("ResourceType")
    public final void initUI() {
        ScrollView main = new ScrollView(this);
        main.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        main.setBackgroundColor(Color.WHITE);

        //根佈局參數
        LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_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(LinearLayout.LayoutParams.WRAP_CONTENT, heightMain);
        layoutParamsImageMain.topMargin = topMargin;
        layoutParamsImageMain.bottomMargin = topMargin;
        layoutParamsImageMain.leftMargin = topMargin;
        layoutParamsImageMain.rightMargin = topMargin;
        layoutParamsImageMain.gravity = Gravity.CENTER_HORIZONTAL;
        //初始化ImageView
        ImageView imageMain = new ImageView(this);
        imageMain.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageMain.setAdjustViewBounds(true);
        imageMain.setImageResource(R.mipmap.laoying);
        layoutRoot.addView(imageMain, layoutParamsImageMain);

        //按鈕佈局
        LinearLayout layoutUser = new LinearLayout(this);
        layoutUser.setLayoutParams(layoutParamsImageMain);
        layoutUser.setOrientation(LinearLayout.HORIZONTAL);
        //editInfo佈局參數
        LinearLayout.LayoutParams layoutParamsEditInfo = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsEditInfo.gravity = Gravity.LEFT;
        layoutParamsEditInfo.leftMargin = dip2px(this, 10);
        layoutParamsEditInfo.rightMargin = dip2px(this, 5);
        layoutParamsEditInfo.weight = 1;

        //初始化textInfo
        TextView textInfo = new TextView(this);
        textInfo.setLayoutParams(layoutParamsEditInfo);
        textInfo.setGravity(Gravity.LEFT);
        textInfo.setTextSize(18);
        textInfo.setText("賬號:");
        layoutUser.addView(textInfo);

        //
        LinearLayout.LayoutParams layoutParamsET = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsET.gravity = Gravity.RIGHT;
        layoutParamsET.leftMargin = dip2px(this, 10);
        layoutParamsET.rightMargin = dip2px(this, 5);
        layoutParamsET.weight = 1;
        //初始化editInfo
        editInfo = new EditText(this);
        editInfo.setLayoutParams(layoutParamsET);
        editInfo.setGravity(Gravity.LEFT);
        editInfo.setHint("請輸入賬號");
        editInfo.setLines(1);
        editInfo.setEllipsize(TextUtils.TruncateAt.END);
        //設置可輸入的最大長度
        InputFilter[] filters = {new InputFilter.LengthFilter(20)};
        editInfo.setFilters(filters);
        editInfo.setTextSize(18);
        layoutUser.addView(editInfo);

        layoutRoot.addView(layoutUser, layoutParamsEditInfo);
        //密碼輸入
        passWord(layoutRoot, layoutParamsImageMain);
        //上邊距(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.setMinimumHeight(minHeight);
        layoutButton.setPadding(leftPadding, topPadding, leftPadding, topPadding);
        layoutButton.setId(100000001);

        //buttonOK佈局參數
        LinearLayout.LayoutParams layoutParamsButtonOK = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.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(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.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);

        buttonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(PureCodeActivity.this, "賬號:" + editInfo.getText().toString() + " 密碼:" + passWord.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //RelativeLayout佈局參數
        LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout layoutBottom = new RelativeLayout(this);
        layoutBottom.setLayoutParams(layoutParamsBottom);

        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);
    }

    private void passWord(LinearLayout layoutRoot, LinearLayout.LayoutParams layoutParamsImageMain) {
        //按鈕佈局
        LinearLayout layoutUser = new LinearLayout(this);
        layoutUser.setLayoutParams(layoutParamsImageMain);
        layoutUser.setOrientation(LinearLayout.HORIZONTAL);
        //editInfo佈局參數
        LinearLayout.LayoutParams layoutParamsEditInfo = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsEditInfo.gravity = Gravity.LEFT;
        layoutParamsEditInfo.leftMargin = dip2px(this, 10);
        layoutParamsEditInfo.rightMargin = dip2px(this, 5);
        layoutParamsEditInfo.weight = 1;

        //初始化textInfo
        TextView textInfo = new TextView(this);
        textInfo.setLayoutParams(layoutParamsEditInfo);
        textInfo.setGravity(Gravity.LEFT);
        textInfo.setTextSize(18);
        textInfo.setText("密碼:");
        layoutUser.addView(textInfo);

        //buttonOK佈局參數
        LinearLayout.LayoutParams layoutParamsET = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsET.gravity = Gravity.RIGHT;
        layoutParamsET.leftMargin = dip2px(this, 10);
        layoutParamsET.rightMargin = dip2px(this, 5);
        layoutParamsET.weight = 1;
        //初始化editInfo
        passWord = new EditText(this);
        passWord.setLayoutParams(layoutParamsEditInfo);
        passWord.setGravity(Gravity.LEFT);
        passWord.setLines(1);
        passWord.setEllipsize(TextUtils.TruncateAt.END);
        passWord.setHint("請輸入密碼");
        //設置可輸入的最大長度
        InputFilter[] filters = {new InputFilter.LengthFilter(20)};
        passWord.setFilters(filters);
        passWord.setTextSize(18);
        layoutUser.addView(passWord);

        layoutRoot.addView(layoutUser, layoutParamsEditInfo);
    }
}

源碼鏈接 純代碼源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章