Android PercentRelativeLayout不采用高度和边距上/下相关属性的百分比
我使用百分比相对布局来定义卡片视图的布局。在定义宽度和水平边距属性时,它可以正常工作。但是,以百分比定义高度和上/下边距对布局没有影响。以下是我的卡片布局的xml:Android PercentRelativeLayout不采用高度和边距上/下相关属性的百分比,android,android-cardview,android-percentrelativelayout,Android,Android Cardview,Android Percentrelativelayout,我使用百分比相对布局来定义卡片视图的布局。在定义宽度和水平边距属性时,它可以正常工作。但是,以百分比定义高度和上/下边距对布局没有影响。以下是我的卡片布局的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://sc
<?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>
所以我认为没有任何解释来解释为什么会发生这种情况。但是我找到了一个可以帮助有类似问题的人的工作 所以百分比相对布局对于宽度百分比来说效果很好。但是,您不能将其与边距开始/左侧百分比或边距结束/右侧百分比耦合,两者(或全部4个)也可以很好地协同工作 因此,对于同样需要定义高度/边距的布局,可以在PercentRelativeLayout内创建垂直方向的线性布局,指定其宽度Percent/marginPercent,将高度设置为0dp,并对线性布局中的元素使用权重属性
我主要在卡片视图中使用它,无论屏幕大小或方向如何,元素只占据屏幕的某一部分。因此我不认为有任何解释可以解释为什么会发生这种情况。但是我找到了一个可以帮助有类似问题的人的工作 所以百分比相对布局对于宽度百分比来说效果很好。但是,您不能将其与边距开始/左侧百分比或边距结束/右侧百分比耦合,两者(或全部4个)也可以很好地协同工作 因此,对于同样需要定义高度/边距的布局,可以在PercentRelativeLayout内创建垂直方向的线性布局,指定其宽度Percent/marginPercent,将高度设置为0dp,并对线性布局中的元素使用权重属性
我主要在卡片视图中使用此选项,无论屏幕大小或方向如何,元素都只占据屏幕的某一部分。已经浏览过此链接。问题是app:layout_marginBottomPercent=“10%”即使我在dp anywhere中的height旁边设置了heightpercent,这个特定的文本视图仍然不可见。我甚至将整个项目升级到sdk版本25,以使用父库25.0.1,但这并没有解决问题。我已经通过了此链接。问题是app:layout_marginBottomPercent=“10%”即使我在dp anywhere中的height旁边设置了heightpercent,这个特定的文本视图仍然不可见。我甚至将整个项目升级到sdk版本25,以使用父库25.0.1,但这并没有解决问题。为了澄清,发生这种情况有一个很好的原因,PercentRelativeLayouts在运行时计算其尺寸,因此当您首次加载布局时,PercentRelativeLayout的高度为0,将高度与匹配父项相结合,得到的高度为0。按照这种逻辑,我不应该在宽度方面面临同样的问题吗?不,当使用“android:layout\u height=“wrap\u content”时,这意味着维度是在运行时计算的,而“android:layout\u width=“match\u parent”'当布局首次加载时,实际上有一个设置的高度,因为percentRelativeLayouts父对象的宽度为match parent,所以它只匹配该宽度,percentRelativeLayout内的所有内容都将其宽度基于该宽度的百分比。为了澄清,发生这种情况有一个很好的原因,PercentRelativeLayout在运行时计算其尺寸,因此当您首次加载布局时,PercentRelativeLayout的高度为0,将其与高度匹配,您得到的高度为0。按照这种逻辑,我是否应该在宽度方面面临同样的问题?不,当您使用“android:layout\u height=“wrap\u content”时'这意味着维度是在运行时计算的,而'android:layout_width=“match_parent”'在布局首次加载时实际上有一个设置的高度,因为percentRelativeLayouts parent的宽度为match parent,它只匹配该宽度,然后,percentRelativeLayout内的所有内容都将宽度基于该宽度的百分比。