Android支持%或有沒有一種方法來近似。我有兩種截然不同的屏幕尺寸,但是按百分比計算,我希望活動中的EditBox對屏幕尺寸和屏幕尺寸的比例具有相同的邊距。如何才能做到這一點。Android如何設置百分比填充/保證金,以便EditText在兩邊都有10%的保證金?
25
A
回答
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>
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 2017-11-17 02:54:48
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. 填充/保證金百分比 - 可能的CSS錯誤?
- 2. 當我以百分比設置保證金時,什麼是100%?
- 3. div填充,保證金?
- 4. 導航保證金/填充
- 5. 增加Chart.js比例填充/保證金
- 6. 黃金比例,是否有填充/保證金?
- 7. 保證金或填充以上的CSS
- 8. Div - 保證金作爲百分比內的百分比
- 9. Android PopUpWindow - 如何設置保證金?
- 10. 保證金 - 右邊小於保證金 - 左邊相等的值
- 11. 更改移動的填充/保證金?
- 12. 保證金/填充地收縮的ImageView
- 13. 更改ButtonText的保證金/填充
- 14. 添加保證金/填充到的RatingBar
- 15. 設置按鈕保證金
- 16. R pdf設置保證金
- 17. 保證金左和位置留下了相同的百分比
- 18. 如何計算MDX中的保證金百分比
- 19. 如何設置一個內格邊距不使用填充/保證金
- 20. 右邊的大保證金
- 21. CSS表默認填充或保證金
- 22. 動態保證金/填充sass
- 23. 添加CSS填充/保證金到Divs
- 24. 火狐/ IE填充/保證金修復
- 25. IE7和IE8填充/保證金問題
- 26. 保證金/填充時,文本換行
- 27. 保證金/填充自定義視圖
- 28. 填充和保證金不起作用
- 29. 單位用於填充和保證金
- 30. 表填充/保證金/不管
和爲'高度'? – 2015-01-02 17:02:44
如果'LinearLayout'中只有1個組件,它將不起作用。 – Raptor 2016-04-15 07:42:10
這適用於LinearLayout中只有一個ListView,並且在LinearLayout中使用'minSdkVersion = 16'添加或不添加'android:orientation =「horizontal」'。對於高度,您只需要layout_height = – rockhammer 2017-01-17 04:50:37