2011-09-20 33 views
2

自定義按鈕(有兩個TextField的)需要開發有兩個標籤的按鈕。我如何在Android

我找到自定義視圖的一些好文章,但我無法想象我如何創造一個myButton類(自定義佈局在其中)擴展按鈕...是可能的..

也在XML一些意見,佈局... 我們該怎麼做?

回答

2

我這樣寫,..我有佈局問題。我不能用兩個按鈕填滿屏幕。 parentlayout填滿屏幕,但我不能這兩個按鈕把應該..

enter image description here

我的按鍵佈局:

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center"> 
     <ImageView 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:id="@+id/xbutton2_icon" /> 
     <TextView 
       android:id="@+id/xbutton2_tv" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       /> 
</LinearLayout> 

而其類:

public XButton2(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     LayoutInflater layoutInflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = layoutInflater.inflate(R.layout.xbutton2, this); 
     icon = (ImageView) view.findViewById(R.id.xbutton2_icon); 
     tv = (TextView) view.findViewById(R.id.xbutton2_tv); 
     init(attrs); 
    } 
    protected void init(AttributeSet attrs) { 
     parseAttributes(attrs); 
     setAttrs(); 
    } 
    protected void parseAttributes(AttributeSet attrs) { 
     TypedArray param = getContext().obtainStyledAttributes(attrs, 
       R.styleable.com_matriksdata_bavul_XButton2); 
     this.text = param 
       .getString(R.styleable.com_matriksdata_bavul_XButton2_text); 
     String str = param 
       .getString(R.styleable.com_matriksdata_bavul_XButton2_icon); 

     if (str != null) { 
      String[] arr = str.split("\\/"); 
      this.iconResorucesID = getResources().getIdentifier(
        getContext().getApplicationContext().getPackageName() + ":" 
          + arr[arr.length - 2] + "/" 
          + arr[arr.length - 1].split("\\.")[0], null, null); 
     } 
     this.textSize = param.getFloat(
       R.styleable.com_matriksdata_bavul_XButton2_textSize, 40); 

     param.recycle(); 
    } 

    protected void setAttrs() { 
     if (text != null) { 
      tv.setText(text); 
      tv.setTextSize(XUtil.convertToPixcell(getContext(), textSize)); 
      // tv.setTextColor(textColor); 
      // tv.setHighlightColor(textSelectedColor); 
     } 
     if (iconResorucesID != 0) 
      icon.setImageResource(iconResorucesID); 

    } 

    public void setChecked(boolean isChecked) { 
     if (isChecked) { 
      // setBackgroundResource(selectedBg); 
      tv.setSelected(true); 
     } else { 
      tv.setSelected(false); 
      // setBackgroundResource(bg); 
     } 
     this.isChecked = isChecked; 
    } 

它是哪裏我用它。

<com.matriksdata.widget.SplitButtonController 
          android:layout_marginLeft="8dip" 
          android:layout_marginRight="8dip" 
          android:layout_width="fill_parent" 
          android:orientation="horizontal" 
          android:layout_height="wrap_content" 
          android:gravity="center_vertical" 

          > 
          <com.matriksdata.widget.XButton2 
            mtx:text="@string/strFlight" 
            mtx:textSize="20" 
            mtx:icon="@drawable/flight_buttonicon" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1"/> 

          <com.matriksdata.widget.XButton2 
            mtx:text="@string/strBus" 
            mtx:textSize="20" 
            mtx:icon="@drawable/bus_buttonicon_gray" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent" 
            android:layout_weight="1" /> 
        </com.matriksdata.widget.SplitButtonController> 
+0

我發現問題來自SplitButtonController。如果我寫SplitButtonController的LinearLayout instread,這個問題就解決了。布我需要處理自定義佈局中的兩個按鈕...我應該重寫什麼方法?是否應該在LinearLAyout中自動? – atasoyh

5

您可以創建自定義視圖。我已經使用佈局按鈕通過自定義的按鈕樣式設置佈局,並增加了兩個textViews吧,這樣說:

<LinearLayout android:id="@+id/customButtonLayout" 
android:layout_height="wrap_content" style="@android:style/Widget.Button" 
android:layout_width="wrap_content"> 
<TextView android:text="First" android:id="@+id/firstTextView" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:textColor="#000"></TextView> 
<TextView android:textColor="#000" android:text="Second" 
    android:layout_height="wrap_content" android:id="@+id/secondTextView" 
    android:layout_width="wrap_content" android:layout_marginLeft="10dp"></TextView> 
</LinearLayout> 

,並在活動中,你可以有這樣的設置不同的字體:

Typeface font=Typeface.createFromAsset(getAssets(),"ARIALN.TTF") ; 
Typeface font2=Typeface.createFromAsset(getAssets(), "COMPCTAN.TTF"); 

TextView firstTextView = (TextView)findViewById(R.id.firstTextView); 
TextView secondTextView = (TextView)findViewById(R.id.secondTextView); 

firstTextView.setTypeface(font); 
secondTextView.setTypeface(font2); 

LinearLayout btnLayout=(LinearLayout) findViewById(R.id.customButtonLayout); 
btnLayout.setOnClickListener(this); 
+1

您可以擴展Button View類並添加它,或者擴展View類並創建一個按鈕佈局,但這確實是最簡單的方法。實際上,按鈕只不過是一個佈局,其中包含一個TextView以及一個9補丁背景。 – DeeV