2010-02-25 20 views
8

È possibile creare un pulsante con un layout xml personalizzato?Pulsante con layout XML personalizzato

ho questo schema:

<?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> 

Ora voglio usare questo su un pulsante. Qualcuno sa come posso fare questo?
Stavo pensando se ho avuto Button.java file che ha esteso Button. E poi setView (R.layout.mylayout.xml); ... ma che è stato a facile, e chiaramente non funziona

saluti Martin

risposta

7

Non è possibile utilizzare letteralmente che il layout sul volto di un Button. È possibile ottenere un aspetto simile utilizzando la proprietà android:drawableRight su un Button, tuttavia.

+0

tutto è così facile. Grazie per le informazioni di oggi! :) – f0rz

20

Ho avuto a che fare con questo recentemente perché volevo mettere due visualizzazioni testuali in un pulsante. Devi scegliere:

  1. Estendere classe Button e usarlo nel layout
  2. utilizzare un layout insted di un pulsante e trascinare all'interno everithing è necessario e renderlo cliccabile aggiungendo questo parametter ad esso:

    android:clickable="true" 
    

Dopo di che è possibile modificare te apperance del layout definendo il

android:background="@drawable/my_background" 

che invia ad esso un volto "tasto-like" e il comportamento

/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

È possibile utilizzare un RelativeLayout di sovrapporre semplicemente il vostro vista pulsante personalizzato in cima un vero pulsante come questo:

<?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> 
Problemi correlati