2017-04-07 3 views
2

従属ビューの可視性がGONEに設定されている場合のTextViewのベースライン制約を再調整する方法はありますか?私は私のレイアウトは次のようになりますGONEするsubtitle2のTextViewの可視性を設定していた場合 enter image description hereConstraintLayout:従属ビューの可視性がGONEに設定されている場合のベースライン制約の再調整

enter image description here

マイレイアウトコード:この場合、

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TEST TITLE" 
     android:textColor="@color/black" 
     android:textSize="24sp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent"/> 

    <TextView 
     android:id="@+id/subtitle1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SUBTITLE 1" 
     android:textSize="11sp" 
     app:layout_constraintBaseline_toBaselineOf="@+id/subtitle2" 
     app:layout_constraintHorizontal_chainStyle="packed" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/subtitleSpace" 
     app:layout_constraintRight_toRightOf="parent"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/subtitleSpace" 
     android:layout_width="12dp" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toRightOf="@+id/subtitle1" 
     app:layout_constraintRight_toLeftOf="@+id/subtitle2"/> 

    <TextView 
     android:id="@+id/subtitle2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="4dp" 
     android:text="SUBTITLE 2" 
     android:textSize="14sp" 
     app:layout_constraintLeft_toRightOf="@+id/subtitleSpace" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/title"/> 

</android.support.constraint.ConstraintLayout> 

、私のレイアウトは次のようになります

私は、依存関係がある場合にベースラインを再調整できるいくつかの制約があるかどうか疑問に思っていますewがありません。

答えて

0

GONEウィジェット(Margins when connected to a GONE widgetを参照)に代替マージンを指定できますが、私はベースラインの代替方法については認識していません。あなたの要件を満たすために

一つの方法は、同じビューの隣に配置されてGONETextViewの同じcharacterisicsを持って0dp見えないTextViewを指定することです。 subtitle1subtitle2は、この新しいTextViewのベースラインに繋がります。新しいTextViewは画面に表示されず、幅もゼロですが、それでもベースラインは維持されます。

subtitle2GONEとすると、subtitle1は中心に移動しますが、垂直位置は維持されます。 (ヌル文字で0dpinvisibleを指定すると、冗長であるが、それは明確なウィジェットが表示されてはならないということになります。)

はここで、これらの変更とXMLである:あなたの提案のための

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    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"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TEST TITLE" 
     android:textColor="#FF000000" 
     android:textSize="24sp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" /> 

    <TextView 
     android:id="@+id/subtitle1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SUBTITLE 1" 
     android:textSize="11sp" 
     app:layout_constraintBaseline_toBaselineOf="@+id/viewForBaseline" 
     app:layout_constraintHorizontal_chainStyle="packed" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/subtitleSpace" 
     app:layout_constraintRight_toRightOf="parent" /> 

    <android.support.v4.widget.Space 
     android:id="@+id/subtitleSpace" 
     android:layout_width="12dp" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toRightOf="@+id/subtitle1" 
     app:layout_constraintRight_toLeftOf="@+id/subtitle2" /> 

    <TextView 
     android:id="@+id/subtitle2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="4dp" 
     android:text="SUBTITLE 2" 
     android:textSize="14sp" 
     android:visibility="gone" 
     app:layout_constraintBaseline_toBaselineOf="@+id/viewForBaseline" 
     app:layout_constraintLeft_toRightOf="@+id/subtitleSpace" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/title" /> 

    <TextView 
     android:id="@+id/viewForBaseline" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="4dp" 
     android:textSize="14sp" 
     android:visibility="invisible" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/title" /> 

</android.support.constraint.ConstraintLayout> 
+0

ありがとう!私は既にあなたが提案したのと同じ解決策を適用しましたが、 'GONE'テキストビューベースラインにいくつかの制約があるかどうか疑問視していました。私は実際にレイアウトに空のビューを残して、複雑さを避け、よりきれいに保つことを望んでいません。 – novachevskyi

+0

私はその質問に対する答えはないと思っていますが、そんなことをするのは理にかなっています。 – Cheticamp

関連する問題