[英]RatingBar in a AlertDialog : How to change the number of stars programmatically?
[英]Cannot change width and number of stars of RatingBar
我正在尝试设置仅包含5个星星的RatingBar
,但它会扩展以填充屏幕的宽度。
我发现了类似的问题:
但是,大多数情况下,解决方案是将width设置为wrap_content
但对我而言,宽度始终是wrap_content
。
这是我的布局。 如果向右滚动到底部,则会看到两个RatingBar
:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/add_spot_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:stretchColumns="1" >
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Distance:" />
<TextView android:id="@+id/distancePopup" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Address:" />
<TextView android:id="@+id/addrPopup" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Type:" />
<TextView android:id="@+id/typePopup" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Terrain:" />
<RatingBar
android:id="@+id/terrainPopup"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:max="5"
android:numStars="5"
android:stepSize="1" />
</TableRow>
<TableRow>
<TextView android:text="Difficulty:" />
<RatingBar
android:id="@+id/difficultyPopup"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:max="5"
android:numStars="5"
android:stepSize="0.1" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Description:" />
<TextView
android:id="@+id/descPopup"
android:gravity="top"
android:lines="2"
android:maxLines="2"
android:scrollHorizontally="false" />
</TableRow>
</TableLayout>
</ScrollView>
看起来是这样的:
我认为这可以解决,将RATINGBAR嵌套在RELATIVELAYOUT中。 我认为这个问题是因为TableLayout的本质是在任何情况下都要填充父级,而RATINGBAR占用父级的宽度。 因此,我解决了将RATINGBAR嵌套在RELATIVELAYOUT内的问题,并在WRAP_CONTENT上同时为RATINGBAR和RELATIVE LAYOUT设置了LAYOUT_WIDTH和LAYOUT_HEIGT。 只有在这种情况下,我才能在TABLELAYOUT内放置一个RATINGBAR。
希望有帮助;)
对于未来的Google员工,
阿米特(Amit)的答案为我解决了这个问题:评分小工具显示的星星太多了。
这是因为必须将小部件设置为android:layout_width="wrap_content"
才能考虑numstars。
使用相对布局作为评分栏的父级,而wrap_content用作评分栏将解决您的问题
在表布局中,您使用像
android:shrinkColumns="1"
android:stretchColumns="1"
在这里,您缩小并拉伸相同的列号。
我删除了
android:stretchColumns="1"
并且它为我工作。
最终布局
<TableLayout
android:id="@+id/add_spot_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1" >
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Distance:" />
<TextView android:id="@+id/distancePopup" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Address:" />
<TextView android:id="@+id/addrPopup" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Type:" />
<TextView android:id="@+id/typePopup" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Terrain:" />
<RatingBar
android:id="@+id/terrainPopup"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:max="5"
android:numStars="5"
android:stepSize="1" />
</TableRow>
<TableRow>
<TextView android:text="Difficulty:" />
<RatingBar
android:id="@+id/difficultyPopup"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:max="5"
android:numStars="5"
android:stepSize="0.1" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView android:text="Description:" />
<TextView
android:id="@+id/descPopup"
android:gravity="top"
android:lines="2"
android:maxLines="2"
android:scrollHorizontally="false" />
</TableRow>
</TableLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.