Android成長之路-編碼實現軟體介面

來源:互聯網
上載者:User

實現一個登陸介面:

相對布局:

package cn.csdn.codeui;import android.app.Activity;import android.os.Bundle;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.EditText;import android.widget.RelativeLayout;import android.widget.TextView;public class LoginRelativeActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initUI();}private void initUI() {RelativeLayout rlayout = new RelativeLayout(this);int id = 100;/**添加一個TextView*/TextView textView1 = new TextView(this);/**android:id=""*/textView1.setId(id);/**android:text="使用者名稱:"*/textView1.setText("使用者名稱:");/**android:layout_width="wrap_content"           android:layout_height="wrap_content"*/RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);/**android:layout_alignParentLeft="true"*/textParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);rlayout.addView(textView1, textParams1);//////////int id1 = 200;EditText userEdit = new EditText(this);userEdit.setId(id1);RelativeLayout.LayoutParams EditParams1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);/**android:layout_toRightOf="id的值"*/EditParams1.addRule(RelativeLayout.RIGHT_OF, id);rlayout.addView(userEdit, EditParams1);//////////int Id = 300;TextView textView2 = new TextView(this);textView2.setId(Id);textView2.setText("密碼 :");RelativeLayout.LayoutParams TextParams2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);TextParams2.addRule(RelativeLayout.BELOW, id1);rlayout.addView(textView2, TextParams2);//////////int Id1 = 400;EditText passEdit = new EditText(this);passEdit.setId(Id1);RelativeLayout.LayoutParams EditParams2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);EditParams2.addRule(RelativeLayout.BELOW, id1);EditParams2.addRule(RelativeLayout.RIGHT_OF, Id);rlayout.addView(passEdit, EditParams2);//////////int Id2 = 500;Button login = new Button(this);login.setId(Id2);login.setText("登陸");RelativeLayout.LayoutParams loginParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);loginParams.addRule(RelativeLayout.BELOW, Id1);loginParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);rlayout.addView(login, loginParams);//////////Button insert = new Button(this);insert.setText("註冊");RelativeLayout.LayoutParams insertParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);insertParams.addRule(RelativeLayout.BELOW, Id1);insertParams.addRule(RelativeLayout.LEFT_OF, Id2);rlayout.addView(insert, insertParams);setContentView(rlayout);}}

 

:

 

表格版面配置:

package cn.csdn.codeui;import android.app.Activity;import android.os.Bundle;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.EditText;import android.widget.TableLayout;import android.widget.TableRow;import android.widget.TextView;public class LoginTableActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initUI();}private void initUI() {///表格版面配置TableLayout tlayout = new TableLayout(this);tlayout.setColumnStretchable(1, true);///行TableRow tableRow1 = new TableRow(this);TextView textView1 = new TextView(this);textView1.setText("使用者名稱:");tableRow1.addView(textView1);EditText userEdit = new EditText(this);tableRow1.addView(userEdit);tlayout.addView(tableRow1);TableRow tableRow2 = new TableRow(this);TextView textView2 = new TextView(this);textView2.setText("密碼:");tableRow2.addView(textView2);EditText passEdit = new EditText(this);tableRow2.addView(passEdit);tlayout.addView(tableRow2);TableRow tableRow3 = new TableRow(this);Button btn0 = new Button(this);btn0.setText("登入");tableRow3.addView(btn0);Button btn1 = new Button(this);btn1.setText("註冊");tableRow3.addView(btn1);tlayout.addView(tableRow3);setContentView(tlayout);}}

 

:

 

線性布局:

package cn.csdn.codeui;import android.app.Activity;import android.os.Bundle;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;public class LoginLinearActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);init();}private void init() {//線性布局LinearLayout linearLayout = new LinearLayout(this);/**android:orientation="vertical"*/linearLayout.setOrientation(LinearLayout.VERTICAL);LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);//////////TextView userText = new TextView(this);userText.setText("使用者名稱:");LayoutParams userTextParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);linearLayout.addView(userText, userTextParams);        //////////EditText userEdit = new EditText(this);LayoutParams userEditParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);linearLayout.addView(userEdit, userEditParams);        //////////TextView passText = new TextView(this);passText.setText("密碼:");LayoutParams passTextParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);linearLayout.addView(passText, passTextParams);        //////////EditText passEdit = new EditText(this);LayoutParams passEditParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);linearLayout.addView(passEdit, passEditParams);        //////////Button login = new Button(this);login.setText("登陸");LayoutParams loginParams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);linearLayout.addView(login, loginParams);        //////////Button insert = new Button(this);insert.setText("註冊");LayoutParams insertParams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);linearLayout.addView(insert, insertParams);setContentView(linearLayout, layoutParams);}}

 

:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.