2011-05-13 16 views
25

アンドロイドサポートは近似する方法がありますか?私は2つの非常に異なる画面サイズを持っていますが、パーセンテージでは、アクティビティのEditBoxを両方の画面サイズに対して同じマージンを画面サイズの割合として使用したいとします。どのようにこれを行うことができます。Android EditTextの両サイドに10%の余白があるように、パディング/マージンのパーセンテージを設定するにはどうすればよいですか?

答えて

32

実際に%の設定をサポートしていません(xmlアニメーションファイルの一部を除いています)。デッドセットでパーセンテージを使用している場合は、私が考えることのできる最良の方法はgetWidthとgetHeightです10進数でそれらを乗算し、setMargin()、またはsetPadding()で結果を設定します。

29

これはのLinearLayoutの中にあなたのEditTextをラップすることにより、XMLで可能です:

<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

と:

ここLayout Editor

は、レイアウト源です'ight'のために? –

+0

'LinearLayout'にコンポーネントが1つしかない場合は動作しません。 – Raptor

+0

これは、LinearLayout内のListViewを1つだけ使用し、 'minSdkVersion = 16'のLinearLayoutに' android:orientation = "horizo​​ntal"を追加してもしなくても動作します。高さの場合は、layout_height = – rockhammer

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

1

ConstraintLayoutで導入されたGuidelineで可能です。

は、たとえば、あなたは、画面の高さの25%であなたのEditTextトップを配置し、左と右の画面幅の20%にすることができます

<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> 
関連する問題