关于android中ratingbar星数不受控制的问题

在尝试创建一个ListView中的评分功能时,遇到RatingBar显示异常的问题。原本设置为显示5颗星,实际显示却超出预期。通过检查XML布局文件,发现ratingbar的布局属性`android:layout_toRightOf="@+id/textView2"`和`android:layout_alignParentRight="true"`导致了星星数量无法正确控制。移除`android:layout_toRightOf="@+id/textView2"`后,RatingBar恢复正常,成功显示5颗星。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天试手ListView,编了个类似打分的东东,但是编好之后却发现其中的ratingbar的星星数量不受控制,设置了星星数量为5,结果却是一长串的。

xml布局和效果图如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:text="TextView"
            android:textSize="25dp" />
        
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:text="TextView" />

        <RatingBar
            android:id="@+id/ratingBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:numStars="5"
            style="?android:attr/ratingBarStyleSmall" 
            android:layout_toRightOf="@+id/textView2"   />
<!--   -->

    </RelativeLayout>

</LinearLayout>

效果图成了这样子:


出来的完全不是想要的5颗星,问题出在哪里呢?找了半天,发现ratingbar也没有什么特别需要设置的地方,想破脑袋,最后发现是由于ratingbar的两边都被限制了, android:layout_toRightOf="@+id/textView2"    android:layout_alignParentRight="true"  ,这样再去控制星星数量就不行了,只能控制其大小了,将布局文件中的android:layout_toRightOf="@+id/textView2"   一句去掉,终于正常了,大笑,也算是个收获最后效果如下:

挺简单的东东,高手勿喷

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值