2010-02-25 5 views
8

Có thể tạo nút có bố cục xml tùy chỉnh không?Nút có bố cục XML tùy chỉnh

Tôi có bố trí này:

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

Bây giờ tôi muốn sử dụng này trên một nút. Bất cứ ai biết làm thế nào tôi có thể làm điều này?
Tôi đã suy nghĩ nếu tôi có tệp Button.java mở rộng Nút. Và sau đó setView (R.layout.mylayout.xml); ... nhưng đó là dễ dàng, và nó rõ ràng không làm việc

Trân Martin

Trả lời

7

Bạn không thể nghĩa là sử dụng bố trí đó trên khuôn mặt của một Button. Bạn có thể đạt được một cái nhìn tương tự bằng cách sử dụng các tài sản android:drawableRight trên một Button, mặc dù.

+0

mọi thứ đều dễ dàng như vậy. Cảm ơn u cho thông tin của ngày hôm nay! :) – f0rz

20

Tôi đã xử lý vấn đề này gần đây vì tôi muốn đặt hai bản xem trước văn bản vào một nút. Bạn phải lựa chọn:

  1. Mở rộng lớp Button và sử dụng nó trong cách bố trí của bạn
  2. Sử dụng Layout insted của một nút và kéo bên trong everithing bạn cần và làm cho nó nhấp được bằng cách thêm parametter này với nó:

    android:clickable="true" 
    

Sau đó bạn có thể sửa te apperance bố cục của bạn bằng cách xác định

android:background="@drawable/my_background" 

để cung cấp cho nó một "nút giống như" khuôn mặt và hành vi

/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

Bạn có thể sử dụng một RelativeLayout chỉ đơn giản là che phủ nút giao diện tùy chỉnh của bạn trên đầu trang của một Nút thực như sau:

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