Percentage width in a RelativeLayout

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

    You can have a look at the new percent support library.

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

    docs

    sample

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

    You can accomplish this via layout weights. A weight dictates how the unclaimed portions of the screen are divided up. Give each EditText a layout_width of 0, and some proportional weight. I.e., give one a weight of 2, and the other a weight of 1 if you want the first to take up twice as much space.

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

    I have solved this creating a custom View:

    public class FractionalSizeView extends View {
      public FractionalSizeView(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      public FractionalSizeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
      }
    
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width * 70 / 100, 0);
      }
    }
    

    This is invisible strut I can use to align other views within RelativeLayout.

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

    You are looking for the android:layout_weight attribute. It will allow you to use percentages to define your layout.

    In the following example, the left button uses 70% of the space, and the right button 30%.

    <LinearLayout
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <Button
            android:text="left" 
            android:layout_width="0dp" 
            android:layout_height="wrap_content" 
            android:layout_weight=".70" /> 
    
        <Button
            android:text="right" 
            android:layout_width="0dp" 
            android:layout_height="wrap_content" 
            android:layout_weight=".30" />
    
    </LinearLayout>
    

    It works the same with any kind of View, you can replace the buttons with some EditText to fit your needs.

    Be sure to set the layout_width to 0dp or your views may not be scaled properly.

    Note that the weight sum doesn't have to equal 1, I just find it easier to read like this. You can set the first weight to 7 and the second to 3 and it will give the same result.

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

    You can use PercentRelativeLayout, It is a recent undocumented addition to the Design Support Library, enables the ability to specify not only elements relative to each other but also the total percentage of available space.

    Subclass of RelativeLayout that supports percentage based dimensions and margins. You can specify dimension or a margin of child by using attributes with "Percent" suffix.

    <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
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_widthPercent="50%"
          app:layout_heightPercent="50%"
          app:layout_marginTopPercent="25%"
          app:layout_marginLeftPercent="25%"/>
    </android.support.percent.PercentFrameLayout>
    

    The Percent package provides APIs to support adding and managing percentage based dimensions in your app.

    To use, you need to add this library to your Gradle dependency list:

    dependencies {
        compile 'com.android.support:percent:22.2.0'//23.1.1
    }
    
    0 讨论(0)
  • 2020-11-22 14:11

    Since PercentRelativeLayout was deprecated in 26.0.0 and nested layouts like LinearLayout inside RelativeLayout have a negative impact on performance (Understanding the performance benefits of ConstraintLayout) the best option for you to achieve percentage width is to replace your RelativeLayout with ConstraintLayout.

    This can be solved in two ways.

    SOLUTION #1 Using guidelines with percentage offset

    <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">
    
        <TextView
            android:id="@+id/host_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Host"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="@+id/host_input" />
    
        <TextView
            android:id="@+id/port_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Port"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="@+id/port_input" />
    
        <EditText
            android:id="@+id/host_input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:inputType="textEmailAddress"
            app:layout_constraintTop_toBottomOf="@+id/host_label"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/guideline" />
    
        <EditText
            android:id="@+id/port_input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:inputType="number"
            app:layout_constraintTop_toBottomOf="@+id/port_label"
            app:layout_constraintLeft_toLeftOf="@+id/guideline"
            app:layout_constraintRight_toRightOf="parent" />
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.8" />
    
    </android.support.constraint.ConstraintLayout>
    

    SOLUTION #2 Using chain with weighted width for EditText

    <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">
    
        <TextView
            android:id="@+id/host_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Host"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="@+id/host_input" />
    
        <TextView
            android:id="@+id/port_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Port"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="@+id/port_input" />
    
        <EditText
            android:id="@+id/host_input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:inputType="textEmailAddress"
            app:layout_constraintHorizontal_weight="0.8"
            app:layout_constraintTop_toBottomOf="@+id/host_label"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/port_input" />
    
        <EditText
            android:id="@+id/port_input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:inputType="number"
            app:layout_constraintHorizontal_weight="0.2"
            app:layout_constraintTop_toBottomOf="@+id/port_label"
            app:layout_constraintLeft_toRightOf="@+id/host_input"
            app:layout_constraintRight_toRightOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    In both cases, you get something like this

    0 讨论(0)
提交回复
热议问题