Percentage width in a RelativeLayout

后端 未结 14 1747
遥遥无期
遥遥无期 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:15

    Interestingly enough, building on the answer from @olefevre, one can not only do 50/50 layouts with "invisible struts", but all sorts of layouts involving powers of two.

    For example, here is a layout that cuts the width into four equal parts (actually three, with weights of 1, 1, 2):

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <View
            android:id="@+id/strut"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:background="#000000" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/strut" >
    
            <View
                android:id="@+id/left_strut"
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:layout_toLeftOf="@+id/strut"
                android:layout_centerHorizontal="true"
                android:background="#000000" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignRight="@+id/left_strut"
                android:text="Far Left" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@+id/left_strut"
                android:text="Near Left" />
        </RelativeLayout>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/strut"
                android:layout_alignParentRight="true"
                android:text="Right" />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-22 14:17

    Just put your two textviews host and port in an independant linearlayout horizontal and use android:layout_weight to make the percentage

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

    This does not quite answer the original question, which was for a 70/30 split, but in the special case of a 50/50 split between the components there is a way: place an invisible strut at the center and use it to position the two components of interest.

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <View android:id="@+id/strut"
            android:layout_width="0dp"
            android:layout_height="0dp" 
            android:layout_centerHorizontal="true"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/strut"
            android:layout_alignParentLeft="true"
            android:text="Left"/> 
        <Button 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/strut"
            android:layout_alignParentRight="true"
            android:text="Right"/>
    </RelativeLayout>
    

    As this is a pretty common case, this solution is more than a curiosity. It is a bit of a hack but an efficient one because the empty, zero-sized strut should cost very little.

    In general, though, it's best not to expect too much from the stock Android layouts...

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

    Check https://github.com/mmin18/FlexLayout which you can use percent or java expression directly in layout xml.

    <EditText
        app:layout_left="0%"
        app:layout_right="60%"
        app:layout_height="wrap_content"/>
    <EditText
        app:layout_left="prev.right+10dp"
        app:layout_right="100%"
        app:layout_height="wrap_content"/>
    
    0 讨论(0)
  • 2020-11-22 14:24

    PercentRelativeLayout is deprecated from Revision 26.0.0 of support Library.

    Google introduced new Layout called ConstraintLayout.

    Add the library as a dependency in your module-level build.gradle file:

         dependencies {
            compile 'com.android.support.constraint:constraint-layout:1.0.1'
          }
    

    just add in a layout file:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        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">
    </android.support.constraint.ConstraintLayout>
    

    Constraints

    Constraints help you keep widgets aligned. You can use anchors, such as the constraint handles shown below, to determine alignment rules between various widgets.

    1. Wrap Content: The view expands as needed to fit its contents.
    2. Match Constraints: The view expands as needed to meet the definition of its constraints after accounting for margins. However, if the given dimension has only one constraint, then the view expands to fit its contents. Using this mode on either the height or width also allows you to set a size ratio.
    3. Fixed: You specify a specific dimension in the text box below or by resizing the view in the editor.
    4. Spread: The views are evenly distributed (after margins are accounted for). This is the default.
    5. Spread inside: The first and last view are affixed to the constraints on each end of the chain and the rest are evenly distributed.
    6. Weighted: When the chain is set to either spread or spread inside, you can fill the remaining space by setting one or more views to "match constraints" (0dp). By default, the space is evenly distributed between each view that's set to "match constraints," but you can assign a weight of importance to each view using the layout_constraintHorizontal_weight and layout_constraintVertical_weight attributes. If you're familiar with layout_weight in a linear layout, this works the same way. So the view with the highest weight value gets the most amount of space; views that have the same weight get the same amount of space.
    7. Packed: The views are packed together (after margins are accounted for). You can then adjust the whole chain's bias (left/right or up/down) by changing the chain's head view bias.
    8. Center Horizontally or Center Vertically: To create a chain of views quickly, select them all, right-click one of the views, and then select either Center Horizontally or Center Vertically, to create either a horizontal or vertical chain
    9. Baseline alignment: Align the text baseline of a view to the text baseline of another view.
    10. Constrain to a guideline: You can add a vertical or horizontal guideline to which you can constrain views, and the guideline will be invisible to app users. You can position the guideline within the layout based on either dp units or percent, relative to the layout's edge.
    11. Adjust the constraint bias: When you add a constraint to both sides of a view (and the view size for the same dimension is either "fixed" or "wrap content"), the view becomes centered between the two constraints with a bias of 50% by default. You can adjust the bias by dragging the bias slider in the Properties window
    12. Set size as a ratio: You can set the view size to a ratio such as 16:9 if at least one of the view dimensions is set to "match constraints" (0dp).

    You can learn more from the official doc.

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

    Update 1

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

    Below quoting google comment:

    This class was deprecated in API level 26.0.0. consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout


    Google introduced new API called android.support.percent

    Then you can just specify percentage to take by view

    Add compile dependency like

    implementation 'com.android.support:percent:22.2.0
    

    in that, PercentRelativeLayout is what we can do a percentage wise layout

     <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">
         <ImageView
             app:layout_widthPercent="50%"
             app:layout_heightPercent="50%"
             app:layout_marginTopPercent="25%"
             app:layout_marginLeftPercent="25%"/>
     </android.support.percent.PercentRelativeLayout>
    
    0 讨论(0)
提交回复
热议问题