我正在开发一个我有RatingBar
的Android应用程序。目前进行测试,我将其值设置在1-5之间。出于某种原因,评级栏始终从中心开始,并且永远不会占用可用的完整空间。以下是它在应用中的外观:
评级栏左边的整个空间是我用XML给出的,但它没有被填满。
评分栏的另一个问题是,如何在RatingBar左侧添加文本。像Text...(XXXXX)
这样的东西。谢谢
代码:
<RatingBar
android:layout_width="135dp"
android:layout_height="40dp"
android:id="@+id/productStars"
android:numStars="5"
android:scaleX="0.3"
android:scaleY="0.3"
android:stepSize="1"
android:layout_alignWithParentIfMissing="false"
android:paddingLeft="20dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/description"
android:layout_alignEnd="@+id/description" />
更新
整个布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:padding="5dip"
android:background="@color/common_action_bar_splitter"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
>
<ImageView
android:layout_width="150dp"
android:layout_height="200dp"
android:id="@+id/productImage"
android:background="@drawable/layout_image"
/>
<TextView
android:id="@+id/comments"
android:layout_width="50dp"
android:layout_height="35dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/abc_input_method_navigation_guard"
android:gravity="left"
android:singleLine="true"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_above="@+id/productStars"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/description"
android:layout_alignStart="@+id/description" />
<TextView
android:id="@+id/productName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="left"
android:scrollHorizontally="true"
android:visibility="visible"
android:layout_toEndOf="@+id/productImage"
android:layout_toRightOf="@+id/productImage"
android:textColor="@color/abc_input_method_navigation_guard"
android:typeface="monospace"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:singleLine="true"
/>
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center_horizontal|top"
android:ellipsize="end"
android:singleLine="true"
android:gravity="left"
android:scrollHorizontally="true"
android:visibility="visible"
android:layout_above="@+id/comments"
android:layout_toEndOf="@+id/productImage"
android:layout_below="@+id/productName"
android:layout_toRightOf="@+id/productImage"
android:textColor="@color/abc_input_method_navigation_guard"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp" />
<RatingBar
android:layout_width="135dp"
android:layout_height="40dp"
android:id="@+id/productStars"
android:numStars="5"
android:scaleX="0.3"
android:scaleY="0.3"
android:stepSize="1"
android:layout_alignWithParentIfMissing="false"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/description"
android:layout_alignEnd="@+id/description" />
<ImageButton
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/swapButton"
android:src="@drawable/swappossible"
android:background="@null"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/productStars"
android:layout_toStartOf="@+id/productStars" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
不要将修正宽度添加到评级栏,并保留wrap_content
。要使评级栏更小,请使用此
style="?android:attr/ratingBarStyleSmall"
将此添加到您的评分栏 根据你的代码,它应该是这样的
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/productStars"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall"
android:stepSize="1"
android:layout_alignWithParentIfMissing="false"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/description"
android:layout_alignEnd="@+id/description"
android:layout_gravity="center"/>
设置高度wrap_content并删除scaleX
和scaleY
答案 1 :(得分:1)
尝试用此
替换您的评级栏<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/productStars"
android:stepSize="1"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall"
android:layout_alignRight="@+id/description"
android:layout_alignEnd="@+id/description" />