2017-04-15 8 views
-1

レイアウトは左に複数、右には最大のものがあります。私は左のアイテムを動的に削除したいが、彼らは垂直に中央に留まるべきである。制約レイアウトで制約が2つのLinearLayoutsとして動作するようにする

<LinearLayout orientation="horizontal" ...> 

    <LinearLayout orientation="vertical" gravity="center_vertical" ...> 
    <View first/> 
    <View second/> 
    <.../> 
    </LinearLayout> 

    <View theBiggest/> 
</LinearLayout> 

が、それは内部のレイアウトなしで、階層の唯一のレベルでフラットConstraintLayoutを使用して、この動作を達成することが可能である:

今、私は2つのLinearLayout Sを使用してそれを達成しますか?

それは次のようになります。

Layout schema

+0

Ehmを動的に追加することができます。単に 'secondView.setVisibility(GONE) 'を使用すると動作しますか?質問を理解したことは確かですか?私はConstraintレイアウトで試してみましたが、この動作を達成するために制約をどのように配置するかを知ることはできません。ビューは常に垂直方向の中央に配置する必要があります。あなたはそのようなレイアウトを作成してください。既存の答えは手動で制約を変更することを示唆していますが、これを行う必要はありません。なぜなら、線形レイアウトに使用する方が簡単だからです。 – Gaket

+0

INVISIBLEへの表示を設定します。 ITはスペースを取るが、表示されません – Zoe

+0

しかし、それは私が得たいものではありません。質問をもう一度見てください。私はそれがあった場所と同じ場所ではなく、中心になるように見たい。まだ質問がない場合は、以下の提案された回答と私のコメントを見てください。 – Gaket

答えて

1

あなたは垂直連鎖を通じてこの機能を実現することができます。以下の2つのレイアウト観察:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <View 
     android:id="@+id/view" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_marginTop="0dp" 
     android:background="@color/black" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginLeft="0dp" 
     app:layout_constraintRight_toLeftOf="@+id/guideline" 
     android:layout_marginRight="0dp" 
     app:layout_constraintBottom_toTopOf="@+id/view2" /> 

    <View 
     android:id="@+id/view2" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@color/black" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="0dp" 
     android:layout_marginLeft="0dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/guideline" 
     android:layout_marginRight="0dp" 
     android:layout_marginTop="0dp" 
     app:layout_constraintTop_toBottomOf="@+id/view" /> 

    <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.5" 
     tools:layout_editor_absoluteX="160dp" 
     tools:layout_editor_absoluteY="0dp" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="@color/colorAccent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="@+id/guideline" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

enter image description here

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <View 
     android:id="@+id/view" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_marginTop="0dp" 
     android:background="@color/black" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginLeft="0dp" 
     app:layout_constraintRight_toLeftOf="@+id/guideline" 
     android:layout_marginRight="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" /> 

    <!--<View 
     android:id="@+id/view2" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@color/black" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="0dp" 
     android:layout_marginLeft="0dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/guideline" 
     android:layout_marginRight="0dp" 
     android:layout_marginTop="0dp" 
     app:layout_constraintTop_toBottomOf="@+id/view" />--> 

    <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.5" 
     tools:layout_editor_absoluteX="160dp" 
     tools:layout_editor_absoluteY="0dp" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="@color/colorAccent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="@+id/guideline" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

enter image description here

差をIは、第2のビューを削除し、app:layout_constraintBottom_toBottomOf="parent"第1図の底制約が変更されていています。 この制約の調整は、左側のセクションからビューを追加または削除するときに動的に行う必要があります。 ConstraintSet

+0

ありがとうございます。私は答えをアップアップしましたが、私の場合はLinearLayoutsを使う方が簡単です。私は左に6つのレイアウトがあり、すべての制約を手動で再作成することは容易ではありません。 – Gaket

関連する問題