![](/img/trans.png)
[英]Android Jetpack Compose width / height / size modifier vs requiredWidth / requiredHeight / requiredSize
[英]Android Jetpack Compose set modifier properties as percentage of device width or height
我希望能夠將所有修改器屬性設置為已知設備寬度或高度的百分比。 這個想法是沒有必要為所有手機——小型、中型或大型手機——支持不同的屏幕尺寸。 所有手機都應具有與當前設備寬度或高度相關的相同可組合尺寸。 我們應該只為tablets
或landscape mode
實現不同的屏幕尺寸。
我知道對於寬度和高度,我們可以使用.fillMaxWidth(0.9f)
,並設置與 Constraints.maxWidth 相匹配的浮點值 [from = 0.0, to = 1.0] 乘以我們指定的分數。
我想指定類似於 dp、sp 的修飾符屬性,而是使用 dw(設備寬度)和 dh(設備高度)之類的屬性。
這是使用androidx.window:window:1.0.0
庫獲取實際設備寬度和高度並將其轉換為Dp的簡單實現。 當您希望最小邊始終與寬度匹配並且最大邊始終與高度匹配時,請使用dw,dh
。 或者當您希望寬度和高度根據設備旋轉而改變時使用adw,adh
:橫向或縱向模式。
要使用該庫,只需包含依賴項
implementation 'com.github.slaviboy:JetpackComposePercentageUnits:1.0.0'
現在我們可以設置offset, padding, width, height, border, font size...
根據設備寬度或高度進行縮放。
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// init device width and height
initSize()
setContent {
// your UI here
Text(
text = "Hello",
fontSize = 0.04.sh,
color = Color.Green,
modifier = Modifier
.offset(x = 0.07.dw, y = 0.2.dh)
.border(
width = 0.01.dw,
color = Color.Red,
shape = RoundedCornerShape(0.05.dw)
)
)
}
}
}
現在一切都根據設備的寬度或高度進行縮放,並且 Composable 將匹配每個手機尺寸。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.