我有一個安卓設備,它的最大寬度和高度以dp單位指定。
- 最大寬度 - 1079
- 最大寬度 - 1079 dp 。
- 最大高度 - 399 dp 。
我想創建一個LinearLayout,它必須在垂直和水平方向上居中出現,其高度為wrap_content長度,寬度為50vw單位。我知道50vw意味著占據50%的寬度大小,但我很難將這個50vw的寬度要求轉換為dp的尺寸。 因此,我應該把版面寬度硬編碼為399/2 dp = 199.5dp,這應該等于50vw?或者我簡單地將設備的最大寬度分成兩半以符合50vw的要求,這樣做是否正確?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"。
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>
android:layout_gravity="center"/span>
android:gravity="center"/span>
android:orientation="垂直">
<LinearLayout
android:layout_width="199.5dp"/span>
android:layout_height="wrap_content"。
android:layout_gravity="center"/span>
android:orientation="垂直">
<TextView
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
android:text="Hello World!"/span>
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
如果我的理解不正確,請告訴我? 謝謝。
uj5u.com熱心網友回復:
如果你使用的是LinearLayout,并且你希望寬度為父級的50%,你可以使用orientation="horizontal",weightSum,layout_weight。下面是一個例子
<LinearLayout
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
android:orientation="水平"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"/span>
android:layout_height="80dp"/span>
android:layout_weight="1"/span>
android:background="#f00" />
</LinearLayout>
如果你使用ConstraintLayout,你也可以通過使用Guideline與百分數,或layout_constraintHorizontal_weight
uj5u.com熱心網友回復:
你可以使用一個假的LinearLayout和當前的LinearLayout,兩個布局的layout_weight都是1。像這樣的東西就可以了:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
xmlns:tools="http://schemas.android.com/tools"。
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"/span>
android:layout_height="match_parent"/span>
android:layout_weight="1">
<TextView .../>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"。
android:layout_height="match_parent"/span>
android:layout_weight="1"/span>
android:background="#000">
</LinearLayout>
</LinearLayout>
</FrameLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/313073.html
標籤:
