java - 如何在android中将vw测量转换为dp?
问题描述
我有一个 android 设备,它的最大宽度和高度以 dp 单位指定。
- 最大宽度 - 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"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="199.5dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
如果我的理解不正确,请告诉我?谢谢。
解决方案
如果您正在使用LinearLayout
并且希望宽度为父级的 50%,则可以使用orientation="horizontal"
, weightSum
, layout_weight
. 这是一个例子
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#f00" />
</LinearLayout>
如果您使用ConstraintLayout
,您还可以使用 with percent 将宽度设置为父宽度的 50% Guideline
,或者layout_constraintHorizontal_weight
推荐阅读
- r - 有效地找到集合差异并生成随机样本
- python - 如何在 Numpy 多维数组的重复索引位置添加值?
- java - 存在于输入数组中
- r - 如何在一列中对选定的数字进行分类,并在 R 的新列中以类别的形式表示它们?
- c - 杂散的 \ [反斜杠] 在 C 代码中是否有任何意义?
- ionic-framework - 浮动文本框设计问题
- javascript - 反应 JS 中的 NodeJS Instagram 私有 API 客户端
- powershell - 是否可以使用先前的 Get 命令输出自动完成读取主机提示。不保存输出文件
- python - 如果我总是遇到同样的错误,我该如何运行 pipenv?
- python - 展平python中字符串列表的列表