2013-04-03 15 views
6

Tôi đang triển khai bàn phím tùy chỉnh trên Android. Tôi vừa đọc tài liệu về "developer.android.com" và xem mẫu bằng bàn phím mềm. Tất cả tôi có thể - nó là để thay đổi nền bàn phím, thay đổi vị trí của các nút, thiết lập keyIcon thay vì keyLabel để chìa khóa.Làm thế nào để thay đổi màu nền của bàn phím cho bàn phím mềm Android?

Nhưng tôi vẫn không thể thay đổi nền và màu của khóa.

Vui lòng viết một số mã mẫu XML hoặc nguồn. Cảm ơn!

mẫu của tôi, nơi tôi thay đổi nền:

public class GBInput extends InputMethodService implements KeyboardView.OnKeyboardActionListener{ 
    ... 
    private GBKeyboardView mInputView; 
    @Override 
     public View onCreateInputView() { 
      mInputView = (GBKeyboardView) getLayoutInflater().inflate(R.layout.input, null); 
      mInputView.setOnKeyboardActionListener(this); 
      mInputView.setKeyboard(mQwertyKeyboard); 
      mInputView.setBackgroundResource(R.color.keyboard_background); 
      return mInputView; 
     } 
    ... 
    } 

Và tôi cần một cái gì đó như thế: enter image description here

hình ảnh cho tất cả các nút - ý tưởng tồi của nó, vì vậy tôi muốn tìm vấn đề tốt hơn.

+0

sẽ được quản lý bởi chuyên gia đồ họa của bạn coz biểu tượng chìa khóa của nó chỉ tôi đoán – Nezam

+0

Câu hỏi của bạn hơi quá rộng; bạn có thể đăng một số xml mà bạn đã chỉnh sửa hay không và những gì không hoạt động như mong đợi? – etienne

+0

đã thêm thông tin vào câu hỏi của tôi .. – yuralife

Trả lời

21

Thêm dòng mã này để input.xml

android:keyBackground="@drawable/samplekeybackground" 

của bạn Vì vậy input.xml bạn nên kết thúc tìm kiếm tương tự như sau.

<com.example.keyboard.LatinKeyboardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/keyboard" 
    android:layout_alignParentBottom="true" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:keyPreviewLayout="@layout/input_key_preview" 
    android:keyBackground="@drawable/samplekeybackground" 
    /> 

Nếu bạn đã có thể sử dụng tốt ở đây thì mẫu nền chính có thể vẽ sẽ tạo ra kết quả như hình ảnh ví dụ của bạn.

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Non focused states --> 
    <item 
     android:state_focused="false" 
     android:state_selected="false" 
     android:state_pressed="false" 
     android:drawable="@drawable/normal" /> 
    <!-- Pressed state --> 
    <item 
     android:state_pressed="true" 
     android:drawable="@drawable/pressed" /></selector> 

Nếu bạn muốn mọi thứ trong xml bạn có thể sử dụng hình dạng xml như sau. drawable/normal.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <stroke android:width="4dp" android:color="#000000" /> 
    <solid android:color="#FFFFFF"/> 
</shape> 

và drawable/pressed.xml

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
     <stroke android:width="4dp" android:color="#A3D9F3" /> 
     <solid android:color="#4ABCE8"/> 
    </shape> 
+0

câu trả lời tuyệt vời, tuy nhiên màu sắc của các phím trong bàn phím có màu trắng. làm thế nào tôi có thể thay đổi màu sắc? – TharakaNirmana

+1

trong drawable/normal.xml của bạn tìm thấy > '' thay đổi #FFFFFF thành màu bạn muốn. xem [ShapeDrawable] (http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape) để biết thông tin chi tiết. – ericharlow

+4

Điều này chỉ hoạt động ở cấp độ KeyboardView, không phải nếu bạn đang cố gắng đặt màu khác cho các phím cụ thể. Bất kỳ ý tưởng nào về cách triển khai trong phạm vi trong bàn phím xml của bạn? –

4
<com.example.keyboard.LatinKeyboardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/keyboard" 
    android:layout_alignParentBottom="true" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:keyPreviewLayout="@layout/input_key_preview" 
    android:keyBackground="@drawable/samplekeybackground" 
    android:keyTextColor="#000000" 
    /> 

chỉ cần thêm dòng này để keyboardView bạn để thay đổi màu chữ thành màu đen

android: keyTextColor = "# 000000"