アンドロイドサポートは近似する方法がありますか?私は2つの非常に異なる画面サイズを持っていますが、パーセンテージでは、アクティビティのEditBoxを両方の画面サイズに対して同じマージンを画面サイズの割合として使用したいとします。どのようにこれを行うことができます。Android EditTextの両サイドに10%の余白があるように、パディング/マージンのパーセンテージを設定するにはどうすればよいですか?
25
A
答えて
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>
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>
を見てみることができます
+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>
関連する問題
- 1. AndroidのActionBarで左余白余白を削除するにはどうすればよいですか?
- 2. 私はルビーのエビに余白を設定するにはどうすればよいですか?
- 3. DataGridTextColumnの余白とその内部のテキストを設定するにはどうすればよいですか?
- 4. AndroidでEditTextに複数の入力タイプを設定するにはどうすればよいですか?
- 5. Sencha Touch 2で右余白を正しく設定するにはどうすればよいですか?
- 6. Androidで画像の周りの余白を削除するにはどうすればよいですか?
- 7. Androidアプリケーションで10分ごとにアラームを設定するにはどうすればよいですか?
- 8. タッチイベントがEditText内にあるかどうかを検出するにはどうすればよいですか?
- 9. iframeの内側から余分な余白を削除するにはどうすればよいですか?
- 10. Androidでキープアライブタイムアウトを設定するにはどうすればよいですか?
- 11. iPhoneのSafariでWebサイトの白い余白を取り除くにはどうすればよいですか?
- 12. パーセンテージを使用してテーブルセルの最大幅を設定するにはどうすればよいですか?
- 13. サービスAndroidを設定するにはどうすればよいですか?
- 14. Windows 10でRuby Cucumberの依存関係を設定するにはどうすればよいですか?
- 15. メニュー項目の余白やパディングを変更するにはどうすればよいですか?
- 16. 行の長さが10を超えないようにするにはどうすればよいですか?
- 17. コンテキストメニューのタイトルを設定するにはどうすればよいですか?
- 18. ブートストラップの水平パネルに余白を追加するにはどうすればよいですか?
- 19. ビューの幅を他の祖先ビューのパーセンテージに設定するにはどうすればいいですか?
- 20. androidどのようにedittextのための隠しキーボードを設定する
- 21. JavaでStringTemplate(ST)で10進数の書式を設定するにはどうすればよいですか?
- 22. アクセシビリティのためにWPFアプリケーションでフォントサイズをパーセンテージで定義するにはどうすればよいですか?
- 23. HTMLテーブルセルのサイズを継承するように設定するにはどうすればよいですか?
- 24. ReSharper/VS2010のパラメータの左余白を調整するにはどうすればよいですか?
- 25. WPFラベルのテキスト周辺の余白を削除するにはどうすればよいですか?
- 26. Markdownで空白が保持されるようにするにはどうすればよいですか?
- 27. EditTextに常にフォーカスがあることを確認するにはどうすればよいですか?
- 28. アクションバーの右上にある設定アイコン(オーバーフローメニュー)を削除するにはどうすればよいですか?
- 29. raphael.jsでフォントテキストを10ピクセルでなく10ポイントに指定するにはどうすればよいですか?
- 30. Android - EditTextフローティングラベルの余白/余白を減らしますか?
と:
ここ
は、レイアウト源です'ight'のために? –
'LinearLayout'にコンポーネントが1つしかない場合は動作しません。 – Raptor
これは、LinearLayout内のListViewを1つだけ使用し、 'minSdkVersion = 16'のLinearLayoutに' android:orientation = "horizontal"を追加してもしなくても動作します。高さの場合は、layout_height = – rockhammer