With introduction of ConstraintLayout you don't have to write either a single line of code or use third-parties.
Here's the example of how to keep 1:1 aspect ratio for your layout using ConstraintLayout:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:background="@android:color/black"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</View>
</android.support.constraint.ConstraintLayout>
A collection of ViewGroups
which can have a constant width to height ratio.
Assign a ratio and never worry about your views looking out of propotions.
Gradle
dependencies {
compile "com.ritesh:ratiolayout:${latestVersion}"
}
Define 'app' namespace on root view in your layout
xmlns:app="http://schemas.android.com/apk/res-auto"
Include this library in your layout
<com.ritesh.ratiolayout.RatioRelativeLayout
android:id="@+id/activity_main_ratio_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:fixed_attribute="WIDTH"
app:horizontal_ratio="1"
app:vertical_ratio="1">
XML Attribut | Programmatical Getters | Description |
---|---|---|
app:fixed_attribute | getFixedAttribute() | Set one side of the layout as fixed, calculating the other based on this |
app:horizontal_ratio | getHorizontalRatio() | Horizontal aspect ratio. |
app:vertical_ratio | getVerticalRatio() | Vertical aspect ratio. |
- RatioFrameLayout
- RatioGridLayout
- RatioLinearLayout
- RatioRelativeLayout
- RatioTableLayout
- setRatio(float horizontalRatio, float verticalRatio) - Sets ratios
- setRatio(FixedAttribute fixedAttribute, float horizontalRatio, float verticalRatio) - Sets ratios along with Fixed Side.
If you have a better idea or way on this project, please let me know, thanks :)
This project is licensed under the MIT License - see the LICENSE.md file for details