【问题标题】:How to covert vw measurement to dp in android?如何在android中将vw测量转换为dp?
【发布时间】:2021-11-01 10:45:12
【问题描述】:

Android Studio Code Snippet

我有一个以 dp 为单位指定其最大宽度和高度的 android 设备。

  • 最大宽度 - 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>

如果我的理解不正确,请告诉我? 谢谢。

【问题讨论】:

    标签: java android android-layout android-activity android-linearlayout


    【解决方案1】:

    如果您使用LinearLayout,并且希望宽度为父级的50%,则可以使用orientation="horizontal"weightSumlayout_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,您还可以使用带有百分比的Guidelinelayout_constraintHorizontal_weight 将宽度设置为父宽度的50%

    【讨论】:

      【解决方案2】:

      您可以将虚拟LinearLayout 与当前LinearLayout 一起使用,两个布局的layout_weight 均为1。这样的事情会起作用:

      <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:orientation="horizontal">
      
              <LinearLayout
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1">
      
                  <TextView .../>
      
              </LinearLayout>
      
              <LinearLayout
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:background="#000">
      
              </LinearLayout>
      
          </LinearLayout>
      </FrameLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        • 2017-07-19
        • 2017-04-10
        • 2012-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多