在android中添加自定义图像时

在android中添加自定义图像时

本文介绍了在android中添加自定义图像时,评级栏变大并被切断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照有关堆栈溢出的答案向 android 项目添加自定义评级栏,这里是:

应该有五颗星.我该如何解决这个问题?

解决方案

set

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

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?

解决方案

set

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

这篇关于在android中添加自定义图像时,评级栏变大并被切断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:23