![](/img/trans.png)
[英]Android how to evaluate view size to keep same proportion in all devices
[英]How can I make Android View position and size be shown always with the same proportion on any screen size?
我嘗試使用以下布局參數來定位按鈕:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Butt1"
android:id="@+id/b1"
android:rotation="-9"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="205dp"
我的想法是稍后使按鈕透明,並保持背景圖像的視覺提示,以使用戶知道那里有一個按鈕。
我已經通過手動定位按鈕和調整按鈕的大小來使其在我的設備上工作,在我的設備上使用margin top 205dp我得到了:
在另一台設備上,按鈕未與背景圖像對齊/調整大小:
關於如何在任何設備上匹配按鈕的位置和大小的想法?
使用<RelativeLayout>
,而不是android:layout_marginTop="205dp"
使用android:layout_below
屬性,以便您可以在按鈕名之后指定按鈕!
例
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" />
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done" />
</RelativeLayout>
Spinner
EdiText
位於EdiText
之后,而Button
位於第二個Spinner
EdiText
之后
如果您希望始終將按鈕放在屏幕的左中間,請嘗試使用此代碼。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true">
<Button
android:id="@+id/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/score"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/play"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" />
<Button
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/score"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/players"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done" />
</LinearLayout>
</RelativeLayout>
像容器一樣使用LinearLayout
並使用RelativeLayout
的屬性將其放在左側(或右側)和中心垂直,以便LinearLayout
內的所有Button都可以從所有屏幕尺寸更改位置。
閱讀我發布的文檔 ,並查看RelativeLayout參數,以便您找到更適合您的解決方案。
義大利語: Credo tu sia italiano, android:layout_below
定義專有的android:layout_below
根據ID
確定具體的要素,然后按ID
marginTop
。 在使用LinearLayout進行維護時,請考慮使用RelativeLayout
容器的專有名稱,然后在schermata上進行分類。
看看這個庫:它使用百分比定位/大小調整功能定義了RelativeLayout
版本。
我想指出的是,使用透明背景並讓窗口的背景圖像提示按鈕是“簡便”的方法,但這是一種不好的做法。 這樣,您就不會向觸摸您的用戶的用戶提供任何視覺反饋,您將被迫使用您的應用上傳大圖像,該大圖像將不可避免地出現偽像,並且無法很好地適應不同的屏幕縱橫比。
您應該嘗試構建一個UI,其中按鈕是實際按鈕,位於屏幕上。
在您的情況下,這不是一件容易的事,因為您希望形狀不規則的按鈕相互重疊(如果您認為它們是矩形的)。 因此,您需要以特殊的/自定義方式處理觸摸。
如果您決定采用其他路線,請查看以下其他問題和解答:
另一種方法是像在HTML map標簽中那樣用坐標映射圖像。 這個 Android庫似乎在Android Image Widget中實現了確切的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.