我知道我的 android 應用程式有兩種設計,但讓我煩惱的是按鈕沒有完全對齊。首先我使用了 3 層線性布局,但是我的 Enter 按鈕看起來不太好。
然后我決定使用相對布局,但這里的問題是中間按鈕變得比它的鄰居大。即使認為我的 Enter 按鈕看起來不錯,但我無法正確安裝按鈕 #3 和按鈕 #1。我試圖制作清除按鈕
layout_toEndOf = button #1
但我的整個設計在這之后就消失了。
如有任何建議或提示,我將不勝感激!!!
線性版面設計:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/layerOne"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
<TextView
android:id="@ id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:text="@string/result"/>
</FrameLayout>
<LinearLayout
android:id="@ id/layerOne"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:orientation="horizontal"
android:layout_above="@id/layerTwo">
<Button
android:id="@ id/buttonZero"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:onClick="pressedZero"
android:text="@string/_0" />
<Button
android:id="@ id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:text="@string/_1"
android:layout_weight="1"
android:onClick="pressedOne"/>
<Button
android:id="@ id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:text="@string/clear"
android:onClick="pressedClear"/>
</LinearLayout>
<LinearLayout
android:id="@ id/layerTwo"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:orientation="horizontal"
android:layout_above="@id/layerThree">
<Button
android:id="@ id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:text="@string/_2"
android:layout_weight="1"
android:onClick="pressedTwo"/>
<Button
android:id="@ id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:text="@string/_3"
android:layout_weight="1"
android:onClick="pressedThree"/>
<Button
android:id="@ id/buttonPlus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:text="@string/plus"
android:onClick="pressedPlus"/>
</LinearLayout>
<LinearLayout
android:id="@ id/layerThree"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@ id/buttonEnter"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:text="@string/enter"
android:onClick="pressedEnter"/>
<Button
android:id="@ id/buttonStar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:text="@string/star"
android:onClick="pressedStar"/>
</LinearLayout>
</RelativeLayout>
相對版面設計

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/buttonClear"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
<TextView
android:id="@ id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:text="@string/result" />
</FrameLayout>
<Button
android:id="@ id/buttonZero"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_above="@id/buttonTwo"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:onClick="pressedZero"
android:text="@string/_0" />
<Button
android:id="@ id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_above="@id/buttonEnter"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:onClick="pressedTwo"
android:text="@string/_2" />
<Button
android:id="@ id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_above="@id/buttonPlus"
android:layout_alignParentEnd="true"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:onClick="pressedClear"
android:text="@string/clear" />
<Button
android:id="@ id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_above="@id/buttonEnter"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_toStartOf="@id/buttonPlus"
android:layout_toEndOf="@id/buttonTwo"
android:onClick="pressedThree"
android:text="@string/_3" />
<Button
android:id="@ id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_above="@id/buttonThree"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_toStartOf="@id/buttonClear"
android:layout_toEndOf="@id/buttonZero"
android:onClick="pressedOne"
android:text="@string/_1" />
<Button
android:id="@ id/buttonPlus"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_above="@id/buttonStar"
android:layout_alignParentEnd="true"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:onClick="pressedPlus"
android:text="@string/plus" />
<Button
android:id="@ id/buttonStar"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:onClick="pressedStar"
android:text="@string/star" />
<Button
android:id="@ id/buttonEnter"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_toStartOf="@id/buttonStar"
android:onClick="pressedEnter"
android:text="@string/enter" />
</RelativeLayout>
uj5u.com熱心網友回復:
你的錯誤是wrap_content用于旁邊的按鈕寬度layout_weight。布局權重會將所有額外空間分配給按鈕,因此從wrap_content您開始影響“額外”空間的計算:單詞“Enter”比數字“0”占用更多空間,因此您剩下的空間更少分布在周圍。
將您的所有layout_width="wrap_content"屬性更改為layout_width="0dp"(無論您在哪里使用layout_weight)。這將確保按鈕的文本內容不會影響它們的最終寬度。
uj5u.com熱心網友回復:
假設您希望前兩個按鈕具有 3 個大小相同的按鈕 - 將 layout_width 設定為 0dp,并將 layout_weight 設定為 1。這將使 LinearLayout 將其分成 3 個大小均勻的部分。
對于底行,在第一個按鈕上將 layout_width 設定為 0dp,將 layout_weight 設定為 2,在第二個按鈕上設定為 1。這將使第一個布局占用的空間是第二個的兩倍,使其等于上面的 2 個按鈕。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/363588.html
上一篇:包含導航圖設計資料的檔案在哪里
