Android: рейтинг не отображается, становится видимым только при касании
В моем приложении Android рейтинговые бары не видны. Он становится видимым только при прикосновении к нему и исчезает, как только я снимаю пальцы.
Мой код:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myratingStars"
android:layout_gravity="center_horizontal|center_vertical|center"
android:numStars="5"/>
Он не отображается в окне дизайна Lollipop emulator/real phone/Android Studio (в режиме API 23), но отображается в эмуляторе KitKat и в окне дизайна Android (в режиме API 15).
Полный файл макета:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="24dp"
android:orientation="vertical">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myratingStars"
android:layout_gravity="center_horizontal|center_vertical|center"
android:numStars="5"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:maxLines="10"
android:id="@+id/myratingTxt"
android:layout_marginTop="12dp"
android:layout_marginBottom="2dp"
android:hint="@string/write_review"/>
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/submitReview"
android:text="@string/review_submit"
android:layout_gravity="center_horizontal|center|center_vertical"
android:textColor="@color/white"
android:onClick="submitReview"
android:layout_marginTop="8dp"
tools:visibility="visible"/>
</LinearLayout>
</LinearLayout>
Ответы
Ответ 1
После небольшого исследования попытался удачи, и это сработало!!! Наконец нашел решение!
Добавление этих атрибутов для спасения моей жизни!!!
android:progressBackgroundTint="@color/colorAccent"
android:progressTint="@color/colorAccent"
android:secondaryProgressTint="@color/colorAccent"
Пример:
<android.support.v7.widget.AppCompatRatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progressBackgroundTint="@color/colorAccent"
android:progressTint="@color/colorAccent"
android:secondaryProgressTint="@color/colorAccent"
android:rating="4" />
Вы можете изменить эти цвета в соответствии с вашими потребностями!
Дополнительная информация:
android: progressTint используется для фактического цвета выполнения
android: secondaryProgressTint используется для вторичного прогресса, когда выбранный частичный рейтинг указывает половину половины цвета, используя этот атрибут.
android: progressBackgroundTint, используемый для остальной части рейтинга, говорит, что 3 из 5, так что последние 2 цветные это!
Счастливое кодирование!! Ешьте пить Android!;)
Ответ 2
В соответствии с этой проблемой в коде google, попробуйте добавить эти строки к вашим свойствам RatingBar
:
android:isIndicator="true"
style="?android:attr/ratingBarStyleIndicator"
Или, возможно, некоторые из этих стилей:
style="?android:attr/ratingBarStyle"
или
style="android:attr/ratingBarStyleSmall"
Как вы можете видеть, нет ответа от разработчиков Google, поэтому я не знаю, почему это странное поведение появляется, но, возможно, это помогает.
Ответ 3
Без всего файла макета невозможно решить эту проблему, но если это происходит только в Lollipop или Marshmallow, это может быть проблема с возвышением. Попробуйте установить рейтинговую полосу "высота: 10 дп"
Ответ 4
yourRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
YourRatingValue = String.valueOf(rating);
Toast.makeText(getActivity(), "Your Rating :" + YourRatingValue, Toast.LENGTH_SHORT).show();
}
});