Skip to content Skip to sidebar Skip to footer

Android Screen-size And Screen-density Image Selection

I am having problems when trying to use multiple horizontal images on different size devices. I have 7 separate Ratingbars, each using a different custom image. I have scaled all t

Solution 1:

You can equally distribute Views across the Screen by using a LinearLayout and layout_weight. For layout_weight to work you have to set layout_width to 0dp. That is normal don't let yourself be confused by that. If you wanted to equally distribute vertically you would have to set layout_height to 0dp. The layout_weight tells the LinearLayout how big a View should be compared to the other ones. In your case we want all Views to be equally distributed so they all need to have the same width. That's the reason why we assign all Views the same layout_weight - 1. You can do a lot more with layout_weight, for example if you had two Views and you wanted the left view to take up two thirds of the screen and the right one to take up one third of the screen you would assign the left one a layout_weight of 2, and the right one a layout_weight of 1.

I have modified your layout accordingly:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"><RatingBarandroid:numStars="1"android:rating="1"android:stepSize="0.01"android:isIndicator="true"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:progressDrawable="@drawable/selector_a"/><RatingBarandroid:numStars="1"android:rating="0.5"android:stepSize="0.01"android:isIndicator="true"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:progressDrawable="@drawable/selector_b"/><RatingBarandroid:numStars="1"android:rating="0.5"android:stepSize="0.01"android:isIndicator="true"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:progressDrawable="@drawable/selector_c"/><RatingBarandroid:numStars="1"android:rating="0.5"android:stepSize="0.01"android:isIndicator="true"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:progressDrawable="@drawable/selector_d"/><RatingBarandroid:numStars="1"android:rating="0.5"android:stepSize="0.01"android:isIndicator="true"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:progressDrawable="@drawable/selector_e"/><RatingBarandroid:numStars="1"android:rating="0.5"android:stepSize="0.01"android:isIndicator="true"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:progressDrawable="@drawable/selector_f"/><RatingBarandroid:numStars="1"android:rating="0.5"android:stepSize="0.01"android:isIndicator="true"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:progressDrawable="@drawable/selector_g"/></LinearLayout></RelativeLayout>

I assume there will eventually be something else in the layout besides the Ratingbars, that's why I already wrapped everything in a RelativeLayout. Because of your custom progressDrawable you might have a few issues with the height of the rating bars since I changed layout_height to wrap_content. If it doesn't work or you want them to have different heights you can set a fixed height for the RatingBars but leave the layout_width as it is.

On a side note: All those layout_gravity and gravity elements you had in your original layout were essentially useless. To align all the RatingBars to the bottom you would only have needed android:gravity="bottom" on the LinearLayout itself.

Post a Comment for "Android Screen-size And Screen-density Image Selection"