Android纯代码布局LinearLayout

前段时间公司要求做个SDK的登录界面,没办法用XML布局了,很头疼。等到要纯代码编界面的时候才发现XML是有多么的好用方便。

没办法啊还是要硬着头皮去把已经编好的布局转换成代码的形式。

弄好后才发觉,其实也不难。


先了解一下什么是LinearLayout

我觉得LinearLayout也就是一个视图的根布局主要的也就是layout_width和layout_height,线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失。

所以简单实用不用太担心,只要设置宽高边距就好,布局就是来约束控件的,把控件放在布局上面就行。

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_horizontal"

android:orientation="vertical"

android:paddingBottom="@dimen/fab_labels_margin"

tools:context="com.example.youhao.xxlogin.tjLoginActivity">

其中要注意的是

android:layout_width

android:layout_height

android:gravity

....这些属性都可以在LinearLayout中找到

LinearLayout layoutRoot =newLinearLayout(this);

layoutRoot.setLayoutParams(layoutParamsRoot);

layoutRoot.setOrientation(LinearLayout.VERTICAL);

现在已经把我们主体的界面约束已经放上,现在要放东西进去了

界面上是

android:id="@+id/login_progress"

style="?android:attr/progressBarStyleLarge"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="8dp"

android:visibility="gone" />

把里面的约束提取出来

LinearLayout.LayoutParams layoutParamsRoot =newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParamsRoot.gravity= Gravity.CENTER;

控件创建出来

mProgressView=newProgressBar(this);

mProgressView.setLayoutParams(newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.WRAP_CONTENT,

LinearLayout.LayoutParams.WRAP_CONTENT));

mProgressView.setBackgroundColor(Color.WHITE);

mProgressView.setBottom(8);

mProgressView.setVisibility(View.GONE);

mLoginFormView=newScrollView(this);

mLoginFormView.setLayoutParams(newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.MATCH_PARENT));

放进根视图去

layoutRoot.addView(mProgressView);

这样就一个控件从创建到布局完成了,布局就像穿衣服一样,每个界面都要穿衣服,不然都不能出门。

复杂一点的界面一件套一件,都能完成对应的效果。




特意从废纸篓捞除了原来的XML布局文件来做个一一对应。

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_horizontal"

android:orientation="vertical"

android:paddingBottom="@dimen/fab_labels_margin"

tools:context="com.example.youhao.xxlogin.tjLoginActivity">

android:id="@+id/login_progress"

style="?android:attr/progressBarStyleLarge"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="8dp"

android:visibility="gone" />

android:id="@+id/login_form"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/email_login_form"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="登录"

android:textAlignment="center"

android:textSize="30dp"/>

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/email"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="@string/prompt_email"

android:inputType="textEmailAddress"

android:maxLines="1"/>

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/password"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="@string/prompt_password"

android:imeActionId="@+id/login"

android:imeActionLabel="@string/action_sign_in_short"

android:imeOptions="actionUnspecified"

android:inputType="textPassword"

android:maxLines="1"/>

android:id="@+id/email_sign_in_button"

style="?android:textAppearanceSmall"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="16dp"

android:text="登录"

android:textStyle="bold" />

android:id="@+id/email_regist_in_button"

style="?android:textAppearanceSmall"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="16dp"

android:text="注册"

android:textStyle="bold" />



对应的布局代码如下

mProgressView=newProgressBar(this);

mProgressView.setLayoutParams(newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.WRAP_CONTENT,

LinearLayout.LayoutParams.WRAP_CONTENT));

mProgressView.setBackgroundColor(Color.WHITE);

mProgressView.setBottom(8);

mProgressView.setVisibility(View.GONE);

mLoginFormView=newScrollView(this);

mLoginFormView.setLayoutParams(newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.MATCH_PARENT));

//根布局参数

LinearLayout.LayoutParams layoutParamsRoot =newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParamsRoot.gravity= Gravity.CENTER;

//根布局

LinearLayout layoutRoot =newLinearLayout(this);

layoutRoot.setLayoutParams(layoutParamsRoot);

layoutRoot.setOrientation(LinearLayout.VERTICAL);

layoutRoot.addView(mProgressView);

//title布局

LinearLayout.LayoutParams layoutParamsTitle =newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParamsTitle.gravity=Gravity.CENTER_HORIZONTAL;

//title

AppCompatTextView title =newAppCompatTextView(this);

title.setText("登录");

title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

title.setTextSize(30);

layoutRoot.addView(title,layoutParamsTitle);

//用户名text布局

LinearLayout.LayoutParams layoutParamsEmil =newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParamsEmil.gravity=Gravity.CENTER_HORIZONTAL;

//用户名text

mEmailView=newAutoCompleteTextView(this);

mEmailView.setHint("Email");

mEmailView.setInputType(0);

mEmailView.setMaxLines(1);

//用户名布局

TextInputLayout inputLayoutEmil =newTextInputLayout(this);

inputLayoutEmil.addView(mEmailView,layoutParamsEmil);

layoutRoot.addView(inputLayoutEmil,newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT));

//密码Text布局

LinearLayout.LayoutParams layourParamsPassword =newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

layourParamsPassword.gravity=Gravity.CENTER_HORIZONTAL;

//密码Text

mPasswordView=newEditText(this);

mPasswordView.setHint("Password");

mPasswordView.setImeActionLabel("Sign in",0);

mPasswordView.setImeOptions(0);

mPasswordView.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);

mPasswordView.setMaxLines(1);

//密码布局

TextInputLayout inputLayoutPassword =newTextInputLayout(this);

inputLayoutPassword.addView(mPasswordView,layourParamsPassword);

layoutRoot.addView(inputLayoutPassword,newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT));

//登录Button布局

LinearLayout.LayoutParams layoutParamsLoginButton =newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParamsLoginButton.topMargin=16;

layoutParamsLoginButton.gravity=Gravity.CENTER_HORIZONTAL;

//登录Button

Button buttonLogin =newButton(this);

buttonLogin.setText("登录");

buttonLogin.setOnClickListener(newOnClickListener() {

@Override

public voidonClick(View view) {

attemptLogin();

}

});

layoutRoot.addView(buttonLogin,layoutParamsLoginButton);

//注册Button布局

LinearLayout.LayoutParams layoutParamsRegistButton =newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParamsLoginButton.topMargin=16;

layoutParamsRegistButton.gravity=Gravity.CENTER_HORIZONTAL;

//注册Button

Button buttonRegist =newButton(this);

buttonRegist.setText("注册");

buttonRegist.setOnClickListener(newOnClickListener() {

@Override

public voidonClick(View view) {

jumpRegist();

}

});

layoutRoot.addView(buttonRegist,layoutParamsRegistButton);

mLoginFormView.addView(layoutRoot);

setContentView(mLoginFormView);

你可能感兴趣的:(Android纯代码布局LinearLayout)