评级栏变得很大,并在Android中添加自定义图像时被切断

时间:2016-11-08 18:32:33

标签: android android-custom-view ratingbar

我按照堆栈溢出的答案将自定义评级栏添加到android项目中,这里是:https://stackoverflow.com/a/32828726/5909396

以防我发布我的代码。

首先,我在drawable文件夹中创建了ratingBar.xml:

<?php 
   $userBirthDate = "1990-10-02"; // You probably use data from database or POST or GET, I setted a static one just for the example 
   $wishedDate = new DateTime($userBirthDate); // Here, you can add any desired date as an argument to DateTime, by default, it'll take the current DateTime.
   // This is for 100 years
   for ($x = 0; $x < 100; $x++){
     echo '<hr>'; // Just so every year will be separated by a html horizontal bar
     $limit = 52; // 52 for a year, but you could do as much as you need right there
     for ($i = 0; $i < $limit; $i++){
       echo $wishedDate->format('Y-m-d H:i:s'); // Write the current date
       echo '<br />'; // Just to switch line
       $wishedDate->modify('+1 week'); // Add one week to the wished date 
     }
   }
?>

然后我在drawable文件夹中创建了ratingbar_empty.xml和ratingbar_filled.xml:

<?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" />

<?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" />

然后我在styles.xml中创建了自定义样式

<?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" />

最后我在布局中添加了ratingBar。我构建它的方式是评级栏位于相对布局内,相对布局内部还有一个文本视图,位于评级栏的顶部(文本视图显示没有评级);

<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>

当我在模拟器上运行应用程序时,星星非常大并且如下所示被截断:

enter image description here

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

2 个答案:

答案 0 :(得分:0)

我创建了一个带有可自定义评级栏的库。您可以设置完整和空白图标及其大小。我希望这会对你有所帮助。

这是一个用法的例子,非常简单:

<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" />

此外,您可以设置侦听器以获取更改值回调。

Logical Rating Bar

答案 1 :(得分:0)

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