Percentage width in a RelativeLayout

后端 未结 14 1746
遥遥无期
遥遥无期 2020-11-22 13:54

I am working on a form layout for a Login Activity in my Android App. The image below is how I want it to look like:

相关标签:
14条回答
  • 2020-11-22 14:28

    You cannot use percentages to define the dimensions of a View inside a RelativeLayout. The best ways to do it is to use LinearLayout and weights, or a custom Layout.

    0 讨论(0)
  • 2020-11-22 14:29

    Update

    As pointed by @EmJiHash PercentRelativeLayout and PercentFrameLayout is deprecated in API level 26.0.0

    Consider Using ConstraintLayout

    Google introduced new API called android.support.percent

    1)PercentRelativeLayout

    2)PercentFrameLayout

    Add compile dependency like

    compile 'com.android.support:percent:23.1.1'
    

    You can specify dimension in percentage so get both benefit of RelativeLayout and percentage

     <android.support.percent.PercentRelativeLayout
             xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent"/>
         <TextView
             app:layout_widthPercent="40%"
             app:layout_heightPercent="40%"
             app:layout_marginTopPercent="15%"
             app:layout_marginLeftPercent="15%"/>
     </android.support.percent.PercentRelativeLayout/>
    
    0 讨论(0)
提交回复
热议问题