Ich habe eine Listenansicht, die Textansicht und ein Kontrollkästchen enthält.Checkbox nicht in Android 4.x sichtbar, aber in Android 5+ sichtbar
Ich zeige die ListView
in einer DialogFragment
. Das Problem besteht darin, dass die Kontrollkästchen für Geräte (Emulator) mit Android-Version 4.1 bis 4.4.4 nicht angezeigt werden. Aber sie erscheinen normalerweise für Android 5.0 und höher. Ich habe versucht, dies für Stunden zu beheben. Aber kein Erfolg.
Kann mir bitte jemand sagen, was mache ich falsch?
[]
Listenansicht:
<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>
Listenansicht Reihe:
<?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 Klasse Aufblasen der Listenansicht:
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();
}
});
}
Ich habe nicht darüber nachgedacht. Danke, ich werde versuchen, das zurück zu dir zu bekommen. – Retr0spect101
Ich habe getan, was Sie gesagt haben, aber immer noch ich Kontrollkästchen ist nicht sichtbar. Ich kann nur den Checkbox-Text sehen. Vielleicht hat es etwas mit Thema zu tun? Rückwärtskompatibilität? – Retr0spect101
müssen Sie möglicherweise Ihren eigenen Adapter schreiben. ArrayAdapter ist nur für sehr einfache Daten nützlich. Es sieht ungefähr wie - http://stackoverflow.com/a/40259628/1856361 aus –