2010-07-20 90 views
0

我想創建這樣的自定義按鈕:Android的自定義按鈕

http://i77.photobucket.com/albums/j74/bertyhell/button.png?t=1279626910

但不起作用 我需要表現出與相對的按鈕原點絕對定位的按鈕上的文本 所有字符串必須更換在運行時

,但下面的按鈕可以是普通按鈕

我怎麼能做到這一點?

如果我通過繼承android.widget.button創建按鈕,我必須自己繪製整個按鈕?

有沒有更簡單的方法?

編輯

好吧,我試圖從相對佈局

這樣的繼承:

package be.smarttelecom.datacheck.views; 

    import android.content.Context; 
    import android.util.Log; 
    import android.widget.RelativeLayout; 
    import android.widget.TextView; 

    public class AlertButton extends RelativeLayout { 

     private TextView roomNumber; 

     public AlertButton(Context context, String roomNumber) { 
      super(context); 
      try{ 
      this.alertId = alertId; 
      // TODO Auto-generated constructor stub 
      this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 

      TextView room = new TextView(context); 
      room.setFocusable(false); 
      room.setText(R.string.room); 
      room.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 

      this.roomNumber = new TextView(context); 
      this.roomNumber.setFocusable(false); 
      this.roomNumber.setId(R.id.roomNumber); 
      this.roomNumber.setTextSize(26); 
      this.roomNumber.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
      this.roomNumber.setText(roomNumber); 

      }catch(Exception ex){ 
       Log.i("test", "problem during construction of alert button: " + ex.getMessage()); 
      } 
     } 

     public void setRoomNumber(String number){ 
      roomNumber.setText(number); 
     } 

     public void setDescription(String description){ 
      this.description.setText(description); 
     } 
    } 

當我添加這樣的:

 TableRow row2 = new TableRow(this); 
     AlertButton alert = new AlertButton(this, "1","14","wc","krist"); 
     row2.addView(alert); 
     container.addView(row2, 0); 

我得到一個空頁面並且完全沒有錯誤

任何想法我做錯了什麼?

如何將文本框放在某些位置? 把它們放在一個絕對佈局中,並添加x和y座標? (絕對佈局是depricated,替代?)

+0

確保你在你的onCreate方法中設置你的Activity的setContentView(容器)或類似的東西。另外,請看一下定義XML中的佈局。它會讓你的相對佈局更容易處理 – CodeFusionMobile 2010-07-20 15:04:30

+0

你也沒有向AlertButton視圖添加按鈕。只是稱這是一個按鈕,並不能使它神奇的工作不幸:) – CodeFusionMobile 2010-07-20 16:16:13

回答

2

我認爲最簡單的方法是創建一個自定義視圖,延伸RelativeLayout。然後,您可以根據需要隨時添加4個TextView,無論您需要什麼定製。爲了使它看起來和像按鈕一樣,可以將寬度和高度設置爲fill_parent,在佈局中添加一個按鈕。確保您的TextView不可調焦,並且該按鈕被髮送到佈局的背面。

或者,您可以將RelativeLayout的自定義視圖中的屬性設置爲clickable = true和focusable = true。然後添加一個drawable作爲您的視圖背景,看起來像一個按鈕。使用選擇器來處理所有聚焦/按下的狀態,就像真正的按鈕一樣。有關如何做到這一點的詳細信息可以在這裏找到:Standard Android Button with a different color

+0

我試圖梅索德1 我編輯後的第一個新的問題 它不顯示自定義控制器 THX的幫助迄今爲止:) – Berty 2010-07-20 14:43:23

+0

@Berty你是否記得在你的activity的onCreate中設置ContentView(容器)? – CodeFusionMobile 2010-07-20 15:02:56

+0

是的,我加入它 我也嘗試之前增加一個TextView和自定義按鈕後 我得到這樣的 http://i77.photobucket.com/albums/j74/bertyhell/errors/custom_button.png 所以按鈕不顯示 我確實設置了setContentView setContentView(R.layout.alerts); – Berty 2010-07-22 07:43:05