rating bar becomes to large and is cut-off when adding custom images in android

久未见 提交于 2019-11-27 08:50:24

问题


I followed an answer on stack overflow to adding a custom rating bar to an android project, here it is: https://stackoverflow.com/a/32828726/5909396

Just in case I am posting my code.

First I created ratingBar.xml in drawable folder:

<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
          android:drawable="@drawable/ratingbar_empty" />
    <item android:id="@android:id/secondaryProgress"
          android:drawable="@drawable/ratingbar_empty" />
    <item android:id="@android:id/progress"
          android:drawable="@drawable/ratingbar_filled" />

Then I created ratingbar_empty.xml and ratingbar_filled.xml also in drawable folder:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_emptyStar" />

<item android:state_focused="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_emptyStar" />

<item android:state_selected="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_emptyStar" />

<item android:drawable="@drawable/ic_emptyStar" />

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_fullStar" />

<item android:state_focused="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_fullStar" />

<item android:state_selected="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_fullStar" />

<item android:drawable="@drawable/ic_fullStar" />

And then I created the custom style in styles.xml

<style name="CustomRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingbar</item>
    <item name="android:minHeight">50dp</item>
    <item name="android:maxHeight">50dp</item>
</style>

And finally I added a ratingBar to layout. The way I structured it is the rating bar is inside a relative layout and there is a textview inside the relative layout as well that is on top of the rating bar (The text view shows if there is no rating);

 <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:orientation="vertical"
            android:layout_gravity="center">
            <RatingBar
                android:id="@+id/ratingRestaurant"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/CustomRatingBar"
                android:numStars="5"
                android:stepSize="0.1"
                android:isIndicator="true"
                android:max="5"
                android:progress="3"
                android:layout_centerVertical="true"
                android:progressTint="#ffe4b331" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="No rating available"
                android:textColor="@android:color/white"
                android:textSize="11dp"
                android:id="@+id/textViewNoRatingAvailable" />
 </RelativeLayout>

When I run the application on the simulator, the stars are really large and are cut-off like below:

There are supposed to be five stars. How can i fix this problem?


回答1:


I made a library with a customizable rating bar. You can set full and empty icon and their size. I hope this helps you.

This is an example of the usage, is very simple:

<com.kabuki5.logicalratingbar.CustomRatingBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:clickable="true"
        app:iconSize="58dp"
        app:imageEmpty="@drawable/ic_heart_empty"
        app:imageFull="@drawable/ic_heart_full"
        app:initRating="5"
        app:maxItems="5" />

Also you can set a listener to get on change value callback.

Logical Rating Bar




回答2:


set

android:layout_width="wrap_content"
android:layout_height="0dp"


来源:https://stackoverflow.com/questions/40494072/rating-bar-becomes-to-large-and-is-cut-off-when-adding-custom-images-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!