2012-12-21 14 views
5

Tôi đang làm việc trên một Dự án Android cho phép người dùng chọn danh sách mục trong Hộp thoại cảnh báo (xem hình ảnh bên dưới).Làm cách nào để thay đổi kích thước hoặc màu của hộp kiểm xoay vòng?

enter image description here

Tôi đang sử dụng android.widget.spinner. Nhưng hộp kiểm không hiển thị rõ ràng.

Tôi đang cố gắng cá nhân hóa hộp kiểm vì loại này theo mặc định.

Vì vậy, có thể thay thế chúng bằng hộp kiểm của riêng tôi?

+0

vâng .. nó .. là :-) bạn cần tạo bản vẽ của riêng mình cho nó – Blundell

+0

yep @Blundell, tôi thử ví dụ dưới đây bởi Avadhani. Chính xác tôi có một hộp kiểm đã thay đổi (kiểm tra) khi tôi nhấp vào nó. Nhưng vấn đề là làm thế nào để tích hợp bố cục vào dự án của tôi. Tệp xml của simple_spinner_item là dạng xem văn bản có kiểu dáng cụ thể. Tôi không thể thay thế nó bằng tệp xml mới – 13KZ

+1

bây giờ bạn sẽ phải sử dụng mục tùy chỉnh của riêng mình và thay thế nó. Tôi đã cập nhật câu trả lời của mình. – Blundell

Trả lời

0

Tạo cho mình một drawable:

my_checkbox.xml

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

    <item android:drawable="@drawable/my_checkbox_pressed" android:state_selected="true"/> 
    <item android:drawable="@drawable/my_checkbox_focused" android:state_focused="true"/> 
    <item android:drawable="@drawable/my_checkbox_pressed" android:state_pressed="true"/> 
    <item android:drawable="@drawable/my_checkbox_default"/> <!-- pressed --> 

</selector> 

này đi trong /drawable/

Sau đó bạn phải tạo bốn drawables, đây sẽ là file png và bạn sẽ muốn tạo một cho mỗi kích thước của màn hình tức là /drawable-mdpi vv

Bạn có thể lấy chúng từ nguồn Android và chỉ sửa đổi chúng nếu nó dễ dàng hơn. (như là một ví dụ xem tại đây http://androiddrawableexplorer.appspot.com/ và tìm kiếm các hộp kiểm)

Sau đó chỉ cần sử dụng như hộp kiểm của mình cho bố cục mục hàng của bạn

<CheckBox 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:button="@drawable/my_checkbox" 
/> 

simple_spinner_item chỉ trông như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
     style="?android:attr/spinnerItemStyle" 
    android:singleLine="true" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:ellipsize="marquee" /> 

Tất cả những gì bạn phải làm là tạo tệp bố cục của riêng bạn gọi là:

my_simple_spinner item.xml đặt nó trong drawable và thêm hộp kiểm tùy chỉnh của bạn:

<LinearLayout /> 
    <TextView /> 
    <CheckBox android:button="@drawable/my_checkbox" /> 
</LinearLayout> 

Sau đó thiết lập này như bố trí cho mỗi hàng và xử lý các lựa chọn hộp kiểm cho mình ... đó là giá của khách hàng.

+0

Cảm ơn @Blundell vì câu trả lời của bạn. Kể từ khi bạn đăng nó, tôi làm việc trên nó, nhưng không có gì. Trong "my_simple_spinner_item", tôi đã thêm ** android: height and width **. Tôi đã có một lỗi Inflate (Binary xml lớp Eroor Inflating lớp Checkbox) – 13KZ

+1

của tôi xấu 'CheckBox' – Blundell

+0

Xin lỗi @Blundell Tôi không hiểu bình luận cuối cùng của bạn !! – 13KZ

0

Bạn muốn thay đổi nền của checkbox phải không? Sau đó làm như sau:

checkBox.setButtonDrawable(<YourClass>.getResources().getDrawable(R.drawable.checkradio)); // where check radio is an xml file as shown below: 

<?xml version="1.0" encoding="utf-8"?> 
    <item android:drawable="@drawable/unselected_option" android:state_checked="false"/> 
    <item android:drawable="@drawable/selected_option" android:state_checked="true"/> 

nơi unselected_option là một hình ảnh để hiển thị khi checkbox không được chọn và selected_option là một hình ảnh để hiển thị checkbox nền khi lựa chọn được kiểm tra.

+0

Cảm ơn câu trả lời của bạn nhưng tôi không cố gắng thay đổi nền của hộp kiểm. Đôi khi người dùng không biết anh ta phải chọn một mục hoặc nhiều hơn. Vì vậy, những gì tôi đang cố gắng làm là làm cho hộp kiểm tập trung hơn mà không thay đổi nền. – 13KZ

+0

ok @zim rồi đặt "@ color/white" thay vì "@ drawable/unselected_option" để hiển thị ban đầu hộp kiểm là sáng –

+0

@zim tìm thấy giải pháp ??? –

0

những gì bạn phải làm là để tạo ra một spinner tùy chỉnh ....

bạn phải thực hiện một tập tin xml chứa TextView và hộp kiểm (theo kích thước yêu cầu của bạn)

sau đó bạn có để thổi phồng xml mới của bạn vào spinner ..

mã để thổi phồng xem tùy chỉnh: đây chỉ là một ví dụ

public View getCustomView(int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater=getLayoutInflater(); 
      View row=inflater.inflate(R.layout.row, parent, false); 
      TextView label=(TextView)row.findViewById(R.id.company); 
      label.setText(strings[position]); 

      TextView sub=(TextView)row.findViewById(R.id.sub); 
      sub.setText(subs[position]); 

      ImageView icon=(ImageView)row.findViewById(R.id.image); 
      icon.setImageResource(arr_images[position]); 

      return row; 
      } 
     } 
1

Hãy thử cách này trong tệp bố cục của bạn.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearlayout" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@color/border" <--- android built-in style. You can define your own whichever you like. Check for it. 
android:orientation="horizontal" > 
<CheckBox 
    android:id="@+id/check" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="2dp" /> 
<TextView 
    android:id="@+id/Textname" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="7dp" 
    android:textColor="#FAFAFA" 
    android:textSize="14dp" 
    android:textStyle="bold" /> 
</LinearLayout> 
1

Tôi không chắc chắn điều này sẽ giúp bạn, nhưng tôi đã làm spinners tùy chỉnh trong một trong những dự án của tôi, nơi tôi có để thiết lập một nền tùy chỉnh và kích thước phông chữ. Tôi có mã như dưới đây.

Bạn có thể dùng thử. Bên string.xml viết:

<string-array name="spinner_array_environtment"> 
     <item>Test</item> 
     <item>Production</item> 
    </string-array> 

bên MainActivity.java của bạn:

public class MainActivity extends Activity { 
Spinner spinner_environment; 

     @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 

       spinner_environment = (Spinner) findViewById(R.id.spinnerview); 
adapter =ArrayAdapter.createFromResource(this,    R.array.spinner_array_environtment,R.layout.spinner_phone); 
      adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
      spinner_environment.setAdapter(adapter); 
} 

bên spinner_phone.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/spinnerTarget" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="13dp" 
    android:textColor="#4C4646" /> 

Hãy thử điều này. Hy vọng nó sẽ giúp bạn.