【问题标题】:PercentRelativeLayout not taking height and margin top/bottom related attributes in percentagePercentRelativeLayout 不采用百分比的高度和边距顶部/底部相关属性
【发布时间】:2016-12-02 11:33:17
【问题描述】:

我使用百分比相对布局来定义卡片视图的布局。在定义宽度和水平边距属性时它工作正常。但是,以百分比定义高度和边距顶部/底部对布局没有影响。这是我的卡片布局的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    app:cardBackgroundColor="@color/cardview_light_background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/exam_card">
    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:textStyle="bold"
            android:textColor="@color/colorPrimaryDark"
            app:layout_widthPercent="60%"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:id="@+id/exam_name"/>
        <TextView
            app:layout_marginLeftPercent="58.5%"
            app:layout_marginStartPercent="58.5%"
            android:textColor="@color/colorPrimary"
            android:textStyle="bold"
            app:layout_widthPercent="20%"
            android:id="@+id/from_test_date"
            android:layout_height="wrap_content"
            android:textSize="28sp"
            />
        <TextView
            app:layout_widthPercent="20%"
            app:layout_marginLeftPercent="56%"
            app:layout_marginStartPercent="56%"
            android:textColor="@color/colorPrimary"
            android:textStyle="bold"
            android:layout_below="@+id/from_test_date"
            android:id="@+id/from_time_year"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            />
        <View
            app:layout_marginLeftPercent="73%"
            app:layout_marginRightPercent="24%"
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="@color/colorPrimaryDark"/>
        <TextView
            app:layout_marginLeftPercent="79.5%"
            app:layout_marginStartPercent="79.5%"
            android:textColor="@color/colorPrimary"
            android:textStyle="bold"
            app:layout_widthPercent="20%"
            android:id="@+id/to_test_date"
            android:layout_height="wrap_content"
            android:textSize="28sp"
            />
        <TextView
            app:layout_marginLeftPercent="77%"
            app:layout_marginStartPercent="77%"
            android:textColor="@color/colorPrimary"
            android:textStyle="bold"
            android:layout_below="@+id/to_test_date"
            app:layout_widthPercent="20%"
            android:id="@+id/to_time_year"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            />
    </android.support.percent.PercentRelativeLayout>

</android.support.v7.widget.CardView>

【问题讨论】:

  • 已经通过此链接。问题是 app:layout_marginBottomPercent="10%" 并且即使我在 dp 任何地方的高度旁边设置了一个高度百分比,那个特定的文本视图甚至都不可见。我什至将整个项目升级到 sdk-version 25 以使用父库 25.0.1,但这并没有解决问题。

标签: android android-cardview android-percentrelativelayout


【解决方案1】:

所以我认为没有任何解释可以解释为什么会发生这种情况。但我找到了一个解决方法,它可能对遇到类似问题的人有所帮助。

所以百分比相对布局适用于 widthPercentage。但是,您不能将它与 margin Start/Left percent 或 margin End/Right percent 结合起来,两者(或全部 4 个)也可以很好地协同工作。

因此,对于必须以类似意义定义高度/边距的布局,您可以在 PercentRelativeLayout 中创建具有垂直方向的线性布局,指定它的宽度百分比/边距百分比,将高度设置为 0dp 并使用权重属性线性布局中的元素。

我主要在卡片视图中使用它,无论屏幕大小或方向如何,元素都只占据屏幕的特定部分。

【讨论】:

  • 澄清一下,发生这种情况是有充分理由的,PercentRelativeLayouts 在运行时计算它们的尺寸,所以当你第一次加载布局时,PercentRelativeLayout 的高度为 0,加上高度为 match_parent你得到的高度为 0。
  • 按照这个逻辑,我不应该在宽度方面面临同样的问题吗?
  • 不,当您使用 'android:layout_height="wrap_content"' 时,这意味着尺寸是在运行时计算的,而 'android:layout_width="match_parent"' 实际上在布局时具有设定的高度首先加载,因为 percentRelativeLayouts 父级的宽度与父级匹配,它只匹配该宽度,然后 percentRelativeLayout 内的所有内容都基于该宽度的百分比。
猜你喜欢
  • 2018-01-01
  • 2014-01-07
  • 1970-01-01
  • 2015-08-12
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 2015-06-15
相关资源
最近更新 更多