2017-08-02 13 views
0

に追加されたときに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

良いである、下に示すような外観を与える PercentRelativeLayout内部 RelativeLayoutを持つ親を埋めるために拡大していません

しかし、PercentRelativeLayoutにTextViewをプログラムで追加すると、RelativeLayoutは親を垂直方向に塗りつぶしませんPercentRelativeLayout、その結果は次のようになります

RelativeLayoutはどのようにしてその高さで親を満たしますか?

+0

これは明らかですか? RelativeLayout **は展開されます(少なくとも、あなたが 'android:layout_width =" match_parent "'を設定した場合は、少なくとも)。 ImageView **はありません**。 –

+0

@ModularSynthいいえ、私は確かめるために黒い背景を置き、それは拡張しません –

+0

@TimCastelijns残念なことに第2の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> 

あなたは2本目のガイドラインを追加し、ガイドラインにとに生成されTextViewを固定でき私がRelativeLayoutに行ったのと同じように、親。

+0

ConstraintLayoutsでパーセンテージを使用できるかどうかはわかりませんでした。私はこれを調べて、最終的にPercentRelativeLayoutsをConstraintsに変換します。乾杯 –

関連する問題