复选框在Android 4.x中不可见,但在Android 5+中可见

时间:2017-03-20 23:28:47

标签: android listview checkbox

我有listview,其中包含textview和一个复选框。

我在ListView中显示DialogFragment。问题是运行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>

Listview行:

<?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();
        }
    });
}

2 个答案:

答案 0 :(得分:0)

为什么不使用 2017/03/20 12:18:02 [error] 3503#0: *7363 upstream prematurely closed connection while reading response header from upstream, client: 111.11.11.111, server: , request: "POST /api/v1/some/url HTTP/1.1", upstream: "http://127.0.0.1:8081/api/v1/some/url", host: "some.website.com"

的文字

这样你的CheckBox项目可能只是

ListView

答案 1 :(得分:0)

我发现了这个问题。 Android 4.x的复选框颜色为黑色,因此它与黑色背景混合。通过使用AppCompatCheckBox而不是CheckBox来修复它。