RatingBar does not display correctly in Android when on the same TableRow as TextView

So, I have Android 1.6 activity using TableLayout. One of the lines has a RatingBar. This is my xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow> <TextView android:text="name" android:padding="3dip"/> <TextView android:text="@+id/name" android:padding="3dip"/> </TableRow> <TableRow> <TextView android:text="tags" android:padding="3dip"/> <TextView android:id="@+id/tags" android:padding="3dip"/> </TableRow> <TableRow> <TextView android:text="rating" android:layout_width="wrap_content"/> <RatingBar android:id="@+id/rating" style="?android:attr/ratingBarStyleIndicator" android:layout_width="wrap_content" android:rating="4" android:numStars="5"/> </TableRow> <TableRow> <TextView android:text="long string" android:padding="3dip"/> <TextView android:text="this is a very very very long string" android:padding="3dip"/> </TableRow> ... </TableLayout> 

I would like to put a text label to the left of the table row, and then have a 5-star rating on the right. But when I do this, the rating value is not displayed correctly, and I have many false empty stars above the indicated maximum of 5:

alt text

If I select the โ€œratingโ€ of the TextView, so the RatingBar is in its own row table, the RatingBar displays as expected, showing 4 out of 5 possible stars:

alt text

Does anyone know if it is possible to have a RatingBar on the same line as a TextView? Or do I need to rethink this layout?

edited to add: I think another problem in the table that displays a long text string is causing the problem. Without the โ€œlong lineโ€ line in the above example, the RatingBar displays correctly on the same line as the โ€œratingโ€ text.

+4
source share
2 answers

fixed it!

I just needed to set the maximum width in the "long row" column, i.e.

 <TableRow> <TextView android:text="long string" android:padding="3dip"/> <TextView android:text="this is a very very very long string" android:layout_width="wrap_content" android:maxWidth="100dip" android:padding="3dip"/> </TableRow> 

I believe that the space in the table cell should be limited in the RatingBar - if it is given too much space by another component, it will expand to fill it, with unexpected consequences.

+2
source

I think the reason why the rating of stars can change is due to the fact that TableRow automatically expands to match its parent (relative to adjacent rows of the table)

You have not indicated your TableLayout element in your question, so you can add this if the following does not solve your problem.

In TableLayout, you can specify shrink and stretch columns. You specify the index of the column that you want to either compress or expand. The first column is index 0.

 <TableLayout android:shrinkColumns="1,0" android:stretchColumns="0,1"></TableLayout> 

You do not need to specify both properties and it is probably easier to experiment with them to determine what works for you. Just keep in mind that if you stretch the columns, I think they stretch to fit the parents, in which case you may find that the column disappears.

I think you will want to compress the second column with your star rating inside it.

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1335905/


All Articles