Как программно добавить рейтинг-бар - PullRequest
0 голосов
/ 31 мая 2018

Как добавить Рейтинговую панель программно для Android?и как установить количество звезд.иногда, даже если я установлю количество звездочек, оно не появится в пользовательском интерфейсе.

Ответы [ 3 ]

0 голосов
/ 31 мая 2018

Вы можете попробовать какую-нибудь библиотеку, чтобы создать рейтинг .. как https://github.com/DreaminginCodeZH/MaterialRatingBar

0 голосов
/ 01 июня 2018

Для добавления панели рейтинга программно необходимо добавить этот код

//parent layout in which you want to add rating bar
LinearLayout linearLayout = findViewById(R.id.d_layout);
RatingBar r = new RatingBar(MainActivity.this);
linearLayout.addView(r);
//how many starts you want to show,parent layout width must be wrap_content
r.setNumStars(6);
r.setRating(Float.parseFloat("1.5"));
0 голосов
/ 31 мая 2018

Ключ для переноса содержимого поверх

// first create a layout 
LinearLayout ll = new LinearLayout(getApplicationContext());
// now you will have to set width and height 
ll.setMinimumWidth(300);
ll.setMinimumHeight(100);
// now init Rating bar
RatingBar rb = new RatingBar(getApplicationContext());
// now set num of stars
rb.setNumStars(5);
// adding ratingBar into the created layout
ll.addView(rb);
// get the current layout
LinearLayout l = findViewById(R.id.linearLayout);
// now add the layout
l.addView(ll);

Вот результат макета

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:id="@+id/mainLayout">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...