2017-08-02 18 views
0

我有一个RelativeLayout一个PercentRelativeLayout内如下面的代码显示的RelativeLayout不扩大,以填补父时的观点被添加到父

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
               xmlns:app="http://schemas.android.com/apk/res-auto" 
               android:layout_width="match_parent" 
               android:layout_height="wrap_content" 
               android:orientation="vertical" 
               android:padding="10dp"> 

    <RelativeLayout 
     android:id="@+id/clickBox" 
     android:layout_height="match_parent" 
     android:background="@color/PaleBlack" 
     android:fillViewport="true" 
     app:layout_widthPercent="35%"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:scaleX="0.8" 
      android:scaleY="0.8" 
      android:src="@mipmap/ic_open"/> 

    </RelativeLayout> 

</android.support.percent.PercentRelativeLayout> 

它给出了一个样子如下图所示,这是很好的 enter image description here

但是,如果我然后以编程方式将一个TextView添加到PercentRelativeLayout,则RelativeLayout不会扩展垂直填充父项PercentRelativeLayout,然后结果如下所示

enter image description here

我该如何解决这个问题,以便RelativeLayout填充父母的身高?

+0

难道不清楚吗? RelativeLayout **不会**展开(至少,如果您设置了'android:layout_width =“match_parent”')。 ImageView **不**。 –

+0

@ModularSynth不,我把一个黑色的背景来确保它不会扩展 –

+0

@TimCastelijns不幸的是没有第二个XML。视图是在布局绘制后通过代码动态添加的 –

回答

0

由于PercentRelativeLayout已被弃用,你可以考虑切换到一个ConstraintLayout的解决方案,像这样一个布局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.constraint.Guideline 
     android:id="@+id/guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.30" /> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="0dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginRight="0dp" 
     android:layout_marginTop="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintHorizontal_bias="0.495" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/guideline" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="0.0"> 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="false" 
      android:layout_centerInParent="true" 
      android:adjustViewBounds="false" 
      app:srcCompat="@mipmap/ic_launcher" /> 

    </RelativeLayout> 

</android.support.constraint.ConstraintLayout> 

你可以添加第二个指引,锚,你正在生成的方针和到TextView父母就像我对RelativeLayout所做的那样。

+0

不知道你可以在ConstraintLayouts中使用百分比。我要研究这个问题并最终将PercentRelativeLayouts转换为约束条件。干杯 –

相关问题