要使LinearLayout適應屏幕旋轉(zhuǎn),您需要將LinearLayout的屬性設(shè)置為"vertical"和"horizontal",以便在橫屏和豎屏模式下都能自適應。以下是如何設(shè)置LinearLayout屬性的示例:
android:orientation
屬性設(shè)置為"vertical"
或"horizontal"
。例如:<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 在這里添加子視圖 -->
</LinearLayout>
android:layout_width
和android:layout_height
屬性設(shè)置為"wrap_content"
。例如:<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 在這里添加子視圖 -->
</LinearLayout>
android:layout_weight
屬性。例如,如果您有兩個子視圖,可以將它們的android:layout_weight
屬性設(shè)置為1
,這樣它們就會平均分配可用空間。<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
通過以上設(shè)置,您的LinearLayout應該能夠在屏幕旋轉(zhuǎn)時自適應并保持原始尺寸。