Android--動態添加控制項

來源:互聯網
上載者:User

標籤:android   style   http   color   io   os   ar   檔案   sp   

    [html]    [html]  package com.mrzhu.edittest;    import android.app.Activity;  import android.os.Bundle;  import android.view.ViewGroup;  import android.widget.RelativeLayout;    public class EditTestActivity extends Activity {      /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          //自訂的帶提示文本的EditText組件          EditTextTest ed = new EditTextTest(this);          //設定從左邊140px處開始輸入文本          ed.setPadding(140, 0, 0, 0);          //裝載一個布局檔案,向這個布局中動態添加一個EditText          RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.main, null);          //建立一個LayoutParams對象          RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);          //設定android:layout_below屬性的值           layoutParams.addRule(RelativeLayout.BELOW, R.id.button2);          //動態添加EditText          relativeLayout.addView(ed, layoutParams);          //設定布局          setContentView(relativeLayout);      }  }   自訂的EditText控制項:[html]  www.2cto.compackage com.mrzhu.edittest;    import android.content.Context;  import android.graphics.Canvas;  import android.graphics.Color;  import android.graphics.Paint;  import android.util.AttributeSet;  import android.widget.EditText;    public class EditTextTest extends EditText {        public EditTextTest(Context context) {          super(context);      }            public EditTextTest(Context context, AttributeSet attrs, int defStyle) {          super(context, attrs, defStyle);      }        public EditTextTest(Context context, AttributeSet attrs) {          super(context, attrs);      }        @Override      protected void onDraw(Canvas canvas) {          Paint paint = new Paint();          paint.setTextSize(20);          paint.setColor(Color.GRAY);          canvas.drawText("輸入提示文本:", 10, getHeight() / 2 + 5, paint);          super.onDraw(canvas);      }  }   main.xml:[html]  <?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/linearlayout"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="horizontal" >        <Button          android:id="@+id/button2"          android:layout_width="80dp"          android:layout_height="80dp"          android:layout_alignParentTop="true"          android:layout_centerHorizontal="true"          android:layout_marginTop="150dp"          android:onClick="click"          android:text="Button" />    </RelativeLayout>   

Android--動態添加控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.