2011-05-13 67 views

回答

32

它並不真正支持按百分比設置值(除了一些xml動畫文件似乎)如果你死了使用百分比設置我可以想到的最佳方式是從Java調用getWidth和getHeight然後將這些乘以你的小數,並用setMargin()或setPadding()設置結果。

29

這是XML可能通過包裝一個的LinearLayout裏面你的EditText:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="10" 
    android:gravity="center" 
    >  

    <EditText 
     android:layout_height="wrap_content" 
     android:layout_width="0dip" 
     android:layout_weight="8" 
     /> 

</LinearLayout> 
+0

和爲'高度'? – 2015-01-02 17:02:44

+0

如果'LinearLayout'中只有1個組件,它將不起作用。 – Raptor 2016-04-15 07:42:10

+0

這適用於LinearLayout中只有一個ListView,並且在LinearLayout中使用'minSdkVersion = 16'添加或不添加'android:orientation =「horizo​​ntal」'。對於高度,您只需要layout_height = – rockhammer 2017-01-17 04:50:37

22

編輯:現在從這個答案的佈局已被棄用。使用Eugene Brusov提出的解決方案。另外,謝謝chancyWu的評論。


現在有一個更好的方法出來與支持庫版本23.0.0(關於時間,對吧?)。您現在可以使用​​或PercentRelativeLayout

如果您希望的EditText是在屏幕的兩邊有10%的保證金寬度的80%,代碼應該是這樣的:

<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"> 

    <EditText 
     android:layout_height="wrap_content" 
     app:layout_widthPercent="80%" 
     app:layout_marginStartPercent="10%" 
     app:layout_marginEndPercent="10%"/> 

</android.support.percent.PercentRelativeLayout> 

您還可以看看PercentLayoutHelper.PercentLayoutParams

+0

自API級別26.1.0開始棄用 – chancyWu 2017-11-17 02:54:48

1

ConstraintLayout中引入了Guideline

例如,你可以在屏幕高度的25%,將您的EditText頂部和左,右的屏幕寬度的20%:

Layout Editor

這裏的佈局來源:

<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"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textPersonName" 
     android:text="Name" 
     app:layout_constraintLeft_toLeftOf="@+id/left_guideline" 
     app:layout_constraintRight_toLeftOf="@+id/right_guideline" 
     app:layout_constraintTop_toTopOf="@+id/top_guideline" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/top_guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.25" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/left_guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.2" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/right_guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.8" /> 

</android.support.constraint.ConstraintLayout>