2010-02-25 108 views
8

是否可以使用自定義xml佈局創建按鈕?帶自定義XML佈局的按鈕

我有這樣的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="1dp" 
    android:background="#7e7e7e"> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:background="#f9f9f9"> 

<TextView 
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:text="ButtText" 
    android:textColor="#000000"> 
</TextView> 

<ImageView 
    android:id="@+id/ImageView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/arrow" 
    android:layout_alignParentRight="true"> 
</ImageView> 

</RelativeLayout> 
</LinearLayout> 

現在我想用這個按鈕上。任何人都知道我可以做到這一點?
我在想如果我有擴展Button的Button.java文件。然後setView(R.layout.mylayout.xml); ...但是,這是爲了方便,而這顯然不工作

問候馬丁

回答

7

你不能隨便使用佈局上Button的臉。不過,您可以使用Button上的android:drawableRight屬性來實現類似的外觀。

+0

一切都很簡單。謝謝你今天的信息! :) – f0rz 2010-02-25 17:26:13

20

最近我一直在處理這個問題,因爲我想在按鈕中放兩個文本視圖。你必須選擇:

  1. 擴展Button類,並在佈局
  2. 使用佈局使用它insted的一個按鈕,並拖動everithing需要,並使其可點擊加入這個parametter它:

    android:clickable="true" 
    

這之後,你可以通過定義

android:background="@drawable/my_background" 
修改佈局的德外觀

給它一個「按鈕樣」面容和行爲

/res/drawable/mi_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:drawable="@color/black"/> 
    <item android:state_pressed="true" android:state_enabled="false" android:drawable="@color/black" /> 
    <item android:drawable="@color/white"/> 
</selector> 
0

可以使用RelativeLayout的簡單疊加之上您的自定義按鈕圖一個真正的按鈕是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- Your custom button layout --> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#7e7e7e" 
     android:padding="1dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#f9f9f9" 
      android:padding="10dp"> 

      <TextView 
       android:id="@+id/TextView01" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:text="ButtText" 
       android:textColor="#000000" /> 

      <ImageView 
       android:id="@+id/ImageView01" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:background="@drawable/jobs_menu" /> 

     </RelativeLayout> 
    </LinearLayout> 
</RelativeLayout>