我想在兩個元素之間固定布局:文本和按鈕。
<RelativeLayout>
<TextView
android:id="@ id/forecast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50sp"
android:textSize="20sp"
android:text="egegegeg"
android:textColor="@color/logo_color"
android:textAlignment="center" />
<LinearLayout
android:id="@ id/forecast_layout"
android:layout_width="match_parent"
android:layout_height="200sp"
>
<ImageView
android:id="@ id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@raw/yaya"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
android:layout_marginHorizontal="50sp"
android:contentDescription="@string/image" />
</LinearLayout>
<Button
android:id="@ id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="120sp"
android:text="@string/search_button"
android:backgroundTint="@color/light_blue"
/>
</RelativeLayout>
應該是這樣的

我嘗試為 LinearLayout 設定 layout_below 和 layout_above,但 LinearLayout 只是變平了。這個想法是這樣的。固定按鈕和文本,以便元素之間的布局大小在不同的螢屏尺寸上發生變化。
uj5u.com熱心網友回復:
我認為這會解決你的問題,關鍵是,你的linearLayout的高度必須是0dp,所以它會越來越小:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@ id/forecast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:text="egegegeg"
android:textColor="@color/logo_color"
android:textAlignment="center"
android:layout_alignParentTop="true"/>
<LinearLayout
android:id="@ id/forecast_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@ id/forecast"
android:layout_above="@id/search">
<ImageView
android:id="@ id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@raw/yaya"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginHorizontal="50dp"
android:contentDescription="@string/image" />
</LinearLayout>
<Button
android:id="@ id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="120dp"
android:text="@string/search_button"
android:backgroundTint="@color/light_blue"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
你應該知道的另一件事是 textSize 必須使用“sp”,而margin 必須使用“dp”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/523518.html
標籤:安卓布局
