【问题标题】:In Android what does the dimension %p mean?在 Android 中,维度 %p 是什么意思?
【发布时间】:2019-07-06 22:12:44
【问题描述】:

我一直在查看developer website 上的一些Android 示例,并且我看到%p 被用作维度。

我在 Google 上进行了搜索,但找不到任何有关其含义的信息。有人知道吗?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    查看keyWidth 文档以获得解释。

    可选的 %p 后缀提供大小 相对于某个父容器

    【讨论】:

    • 可以把它想象成:"percent (%) of (P)arent" 宽度,尽管相对大小不一定超过 100。
    【解决方案2】:
    1. 50% 是动画视图的中心(相对于视图)。
    2. 50%p 是视图父级的中心(相对于父级)。
    3. 50 距离动画视图的顶部/左侧 50 像素(绝对值)。

    【讨论】:

      【解决方案3】:

      @Cheryl Simon 和@zero_cool 的答案是正确的。
      为了便于理解,我添加了演示

      布局代码

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          >
      
          <Button
              android:id="@+id/button_animate"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Animate"
              />
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="// A gray view with 400dp height"
              android:layout_marginTop="20dp"
              />
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="200dp"
              android:background="#aaa"
              android:orientation="horizontal"
              >
      
              <TextView
                  android:layout_width="60dp"
                  android:layout_height="50dp"
                  android:background="#f00"
                  android:text="50dp height View"
                  android:layout_marginStart="10dp"
                  />
      
              <TextView
                  android:id="@+id/image_1"
                  android:layout_width="60dp"
                  android:layout_height="50dp"
                  android:layout_marginStart="20dp"
                  android:background="#f00"
                  android:src="@mipmap/ic_launcher"
                  android:text="animate with %"
                  />
      
              <TextView
                  android:id="@+id/image_2"
                  android:layout_width="60dp"
                  android:layout_height="50dp"
                  android:layout_marginStart="20dp"
                  android:background="#f00"
                  android:text="animate with %p"
                  />
      
          </LinearLayout>
      </LinearLayout>
      

      动画文件
      slide_in.xml

      <?xml version="1.0" encoding="utf-8"?>
      <set xmlns:android="http://schemas.android.com/apk/res/android">
          <translate
              android:duration="5000"
              android:fromYDelta="25%"
              android:toYDelta="0"/>
      </set>
      

      slide_in_with_p.xml

      <?xml version="1.0" encoding="utf-8"?>
      <set xmlns:android="http://schemas.android.com/apk/res/android">
          <translate
              android:duration="5000"
              android:fromYDelta="25%p"
              android:toYDelta="0"/>
      </set>
      

      活动代码

      button_animate.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Animation slide = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_in);
              Animation slideWithP = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_in_with_p);
      
              viewAnimateWithPercent.startAnimation(slide);
              viewAnimateWithPercentP.startAnimation(slideWithP);
          }
      });
      

      DEMO

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-21
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 1970-01-01
        相关资源
        最近更新 更多