2017-03-20 78 views
0

我有listview,其中包含textview和複選框。複選框在Android 4.x中不可見,但在Android 5+中可見

我在DialogFragment中顯示ListView。問題是複選框不顯示運行Android版本4.1到4.4.4的設備(模擬器)。但他們通常顯示爲Android 5.0+。我一直在努力解決這個問題好幾個小時。但沒有成功。

有人可以告訴我我做錯了什麼?

[Android 4.1, No Checkbox]

Android 5.1, Checkbox visible

列表視圖:

<RelativeLayout 
     android:id="@+id/rl_cats_overlay" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tv_game_info"> 

     <ListView 
      android:id="@+id/lv_select_cats" 
      android:layout_width="wrap_content" 
      android:layout_height="200dp" 
      android:layout_marginLeft="35dp" 
      android:layout_marginRight="35dp" 
      android:fadeScrollbars="false" /> 

     <TextView 
      android:id="@+id/tv_overlay_pro" 
      android:layout_width="wrap_content" 
      android:layout_height="200dp" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginLeft="35dp" 
      android:layout_marginRight="35dp" 
      android:background="#A6000000" 
      android:gravity="center" 
      android:text="Available with \n'Quiz Time Pro'" 
      android:textColor="@color/android_green" 
      android:textSize="24sp" /> 

    </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="wrap_content" 
android:orientation="horizontal"> 

<CheckBox 
    android:id="@+id/cb_row_item" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"/> 

<TextView 
    android:id="@+id/tv_row_item" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/cb_row_item" 
    android:layout_alignBottom="@+id/cb_row_item" 
    android:layout_marginLeft="12dp" 
    android:layout_marginStart="12dp" 
    android:layout_toEndOf="@+id/cb_row_item" 
    android:layout_toRightOf="@+id/cb_row_item" 
    android:text="Category name goes here" 
    android:textSize="15sp" /> 

</RelativeLayout> 

DialogFragment類充氣列表視圖:

public class SelectGameModeDialogFragment extends DialogFragment { 

boolean[] checkedCategories = new boolean[Constants.CATEGORIES.length + 1]; 
boolean[] checkedDifficulty = new boolean[Constants.DIFFICULTY_LEVELS.length + 1]; 

List<String> categories = new ArrayList<>(); 
List<String> difficultyLevels = new ArrayList<>(); 

TextView gameInfoTV; 
Spinner spinner; 

public static SelectGameModeDialogFragment newInstance(String title) { 
    SelectGameModeDialogFragment frag = new SelectGameModeDialogFragment(); 
    Bundle args = new Bundle(); 
    args.putString("title", title); 
    frag.setArguments(args); 
    frag.setStyle(DialogFragment.STYLE_NO_FRAME, 0); 
    return frag; 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    categories.addAll(Arrays.asList(Constants.CATEGORIES)); 
    difficultyLevels.addAll(Arrays.asList(Constants.DIFFICULTY_LEVELS)); 
    return inflater.inflate(R.layout.select_game_mode, container); 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    final ListView lvCategories = (ListView) view.findViewById(R.id.lv_select_cats); 

    gameInfoTV = (TextView) view.findViewById(R.id.tv_game_info); 
    gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[0]); 

    final TextView proTV = (TextView) view.findViewById(R.id.tv_overlay_pro); 
    final Button btnPlay = (Button) view.findViewById(R.id.btn_restart_game); 

    spinner = (Spinner) view.findViewById(R.id.spinner_gamer_categories); 
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.game_types_spinner, R.layout.spinner_item); 
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item); 
    spinner.setAdapter(adapter); 
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
      if (pos == Constants.MODE_MILLIONAIRE 
        || pos == Constants.MODE_UNLIMITED 
        || pos == Constants.MODE_20QUESTIONS 
        || pos == Constants.MODE_60SECONDS) { 
       ViewGroup.LayoutParams params = lvCategories.getLayoutParams(); 
       params.height = 0; 
       lvCategories.setLayoutParams(params); 
       proTV.setVisibility(View.GONE); 
      } else if (pos == Constants.MODE_CATEGORIES) { 
       MultipleCategoryListAdapter adapter = new MultipleCategoryListAdapter(getActivity(), categories); 
       lvCategories.setAdapter(adapter); 
       ViewGroup.LayoutParams params = lvCategories.getLayoutParams(); 
       params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics()); 
       lvCategories.setLayoutParams(params); 
      } else if (pos == Constants.MODE_DIFFICULTY) { 
       SingleCategoryListAdapter adapter = new SingleCategoryListAdapter(getActivity(), difficultyLevels); 
       lvCategories.setAdapter(adapter); 
       ViewGroup.LayoutParams params = lvCategories.getLayoutParams(); 
       params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics()); 
       lvCategories.setLayoutParams(params); 
      } 
      lvCategories.setVisibility(View.INVISIBLE); 
      if (pos == Constants.MODE_MILLIONAIRE) { 
       gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[0]); 
      } else if (pos == Constants.MODE_20QUESTIONS) { 
       gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[1]); 
      } else if (pos == Constants.MODE_CATEGORIES) { 
       gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[2]); 
       lvCategories.setVisibility(View.VISIBLE); 
      } else if (pos == Constants.MODE_60SECONDS) { 
       gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[3]); 
      } else if (pos == Constants.MODE_UNLIMITED) { 
       gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[4]); 
      } else if (pos == Constants.MODE_DIFFICULTY) { 
       gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[5]); 
       lvCategories.setVisibility(View.VISIBLE); 
      } 
     } 

     public void onNothingSelected(AdapterView<?> parent) { 
     } 
    }); 

    btnPlay.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      int spinnerPosition = spinner.getSelectedItemPosition(); 
      ArrayList<String> cat = new ArrayList<>(); 
      if (spinnerPosition == Constants.MODE_MILLIONAIRE) { 
       goToPlayingFragmentWithCategories(cat, Constants.MODE_MILLIONAIRE); 
      } else if (spinnerPosition == Constants.MODE_20QUESTIONS) { 
       goToPlayingFragmentWithCategories(cat, Constants.MODE_20QUESTIONS); 
      } else if (spinnerPosition == Constants.MODE_CATEGORIES) { 
       for (int i = 0; i < checkedCategories.length; i++) { 
        if (checkedCategories[i]) { 
         cat.add(categories.get(i)); 
        } 
       } 
       goToPlayingFragmentWithCategories(cat, Constants.MODE_CATEGORIES); 
       Arrays.fill(checkedCategories, false); 
       System.out.println(); 
      } else if (spinnerPosition == Constants.MODE_60SECONDS) { 
       goToPlayingFragmentWithCategories(cat, Constants.MODE_60SECONDS); 
      } else if (spinnerPosition == Constants.MODE_UNLIMITED) { 
       goToPlayingFragmentWithCategories(cat, Constants.MODE_UNLIMITED); 
      } else if (spinnerPosition == Constants.MODE_DIFFICULTY) { 
       String diff = ""; 
       for (int i = 0; i < 3; i++) { 
        if (checkedDifficulty[i]) { 
         diff = difficultyLevels.get(i); 
        } 
       } 
       goToPlayingFragmentWithCategories(cat, Constants.MODE_DIFFICULTY, diff); 
      } 
      dismiss(); 
     } 
    }); 

    Button btnBack = (Button) view.findViewById(R.id.btn_game_mode_back); 
    btnBack.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      dismiss(); 
     } 
    }); 
} 

回答

0

我已經找到了問題。 Android 4.x的複選框顏色爲黑色,因此它與黑色背景混合。通過使用AppCompatCheckBox而不是CheckBox來修復它。

0

爲什麼不只是使用CheckBox的文字?

這樣,你的ListView項目可能僅僅是

<?xml version="1.0" encoding="utf-8"?> 
<CheckBox 
     android:id="@+id/cb_row_item" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:text="Category name goes here" 
     android:textSize="15sp"/> 
+0

我沒有想到這一點。謝謝,我會盡力回覆你。 – Retr0spect101

+0

我做了你說的,但仍然我複選框不可見。我只能看到複選框文本。可能它與主題有關嗎?向後兼容? – Retr0spect101

+0

你可能需要看看編寫你自己的適配器。 ArrayAdapter只對非常簡單的數據有用。它看起來像 - http://stackoverflow.com/a/40259628/1856361 –