3

我试图设置一个RatingBar只有 5 颗星,但它会扩展以填充屏幕的宽度。

我发现了类似的问题:

但是,大多数时候解决方案是将宽度设置为,wrap_content但对我来说宽度一直是wrap_content.

这是我的布局。如果您向右滚动到底部,您将看到两个RatingBars:

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

这是它的样子:

4

4 回答 4

3

我认为这可以通过将 RATINGBAR 嵌套在 RELATIVELAYOUT 中来解决。我认为这个问题是因为 TableLayout 的本质是在任何情况下都填充父级,而 RATINGBAR 采用父级的宽度。因此,我解决了将 RATINGBAR 嵌套在 RELATIVELAYOUT 中并在 WRAP_CONTENT 上为两者(RATINGBAR 和 RELATIVE LAYOUT)设置 LAYOUT_WIDTH 和 LAYOUT_HEIGT 的问题。只有在这种情况下,我才能在 TABLELAYOUT 中有一个 RATINGBAR。

希望有所帮助;)

于 2015-12-15T21:05:57.467 回答
1

对于未来的 Google 员工,

Amit 的回答帮助我解决了这个问题:评分小部件显示的星星太多。

这是因为小部件必须设置为android:layout_width="wrap_content"要考虑 numstars。

于 2014-10-13T09:04:28.203 回答
1

使用相对布局作为评分栏的父级,使用 wrap_content 作为评分栏将解决您的问题

于 2016-01-07T06:30:29.777 回答
0

在表格布局中,您使用的是

    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>

于 2013-03-14T02:03:34.667 回答