LoginSignup
354
302

More than 5 years have passed since last update.

ConstraintLayoutをもう少し深く知ってみる

Last updated at Posted at 2017-09-14

shibuya.apk #18
https://shibuya-apk.connpass.com/event/64610/


Google I/OConstraintLayoutConstraintLayuot
output_sicle.gif ConstraintLayout使


output.gif
Google I/O


https://github.com/takahirom/constraint-layout-samples



名前 説明
Fixed 設定されている大きさ。width="16dp"など
Match Constraints Constraintを満たす最大の大きさ。XMLファイルでは0dpがこれの意味で、width="0dp"となっていたらMatch Constraintだと思うといいと思います
Wrap Content コンテンツの大きさに依存する。(画像サイズや文字数など)
Match Parent ConstraintLayoutでは利用されるべきではない

Bias


wrap_contentfixedbias
50

bias.gif

ConstraintSet使
val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayout)
val bias = 0.5F
constraintSet.setVerticalBias(R.id.button, bias)
constraintSet.applyTo(constraintLayout)

ConstraintLayout
image.png

Chain


View
LinearLayoutweight=1
ViewLinearLayout


(Chain StyleCHAIN_SPREAD -> CHAIN_SPREAD_INSIDE -> CHAIN_PACKED)
chain.gif
xml
app:layout_constraintHorizontal_chainStyle="spread_inside"

width0dpmatch constraint
image.png
weight
app:layout_constraintHorizontal_weight="1"

GuideLine



KeyLine

HelpersAdd GlideLine

DP
20dp50%
%UI
xml
app:layout_constraintGuide_begin="20dp"

以下のようにapp:layout_constraintGuide_percentを使うように修正することがで利用することができます。割合は0〜1で指定します。

app:layout_constraintGuide_percent="0.5"

ConstraintLayoutはViewがgoneのときの挙動もサポートしている


ConstraintLayoutViewGONEView0x0Margin0
gone_2.gif
GONEViewViewgone
app:layout_goneMarginLeft="200dp"

gone.gif

1.1.0 Beta-1

新しく追加された機能について紹介していきます。

Barrier

なぜ使うか?

以下のようなレイアウトの時に"Settings"という文字列がめっちゃ長くなる可能性がある時、どうやって実装しますか??

通常時 長くなった時
image.png image.png

SettingsBarrier

使


CameraSettingsTextViewBarrier使
Helpers -> Add Vertical barrier(BarrierB)

image.png Barrier
add.gif BarrierCameraSettingsTextViewrightend
image.png
EditTextConstraintBarrierOK

constraint.gif


testbarrier.gif

Group

使


View使
(visibilityelevation)

使


ConstraintLayoutAdd Group

image.png GroupView
group.gif
groupidView
findViewById<Group>(R.id.group).visibility = View.GONE

PlaceHolder

PlaceHolderは用意しておくことで、簡単にウィジェットの内容を置き換えることができます。

placeholder.gif

たったこれだけ。最初はなぜこんな動きができるのか意味不明でした。

val onClickListener: (View) -> Unit = { view ->
     TransitionManager.beginDelayedTransition(root as ViewGroup)
     placeholder.setContentId(view.id)
}
imageA.setOnClickListener(onClickListener)
imageB.setOnClickListener(onClickListener)
imageC.setOnClickListener(onClickListener)
imageD.setOnClickListener(onClickListener)

使い方


使Chain

image.png
xmlPlaceHolderUI(AndroidStudioUI)
app:content="@+id/image_d"PlaceHolderView
        <android.support.constraint.Placeholder
            android:id="@+id/placeholder"
            ...
            app:content="@+id/image_d"
            ...  />

image.png

contentIdViewChainViewView
TransitionManager.beginDelayedTransition使Transition
val onClickListener: (View) -> Unit = { view ->
     TransitionManager.beginDelayedTransition(placeHolderBinding.root as ViewGroup)
     placeholder.setContentId(view.id)
}
imageA.setOnClickListener(onClickListener)
imageB.setOnClickListener(onClickListener)
imageC.setOnClickListener(onClickListener)
imageD.setOnClickListener(onClickListener)

Percent Dimension

以下で%(割合)で大きさを指定できます。(現状、AndroidStudioのUI上で設置できない?)
PercentRelativeLayoutの置き換えが快適になりそうです。

android:layout_width="0dp"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"

Google I/Oのアニメーションについて



output_sicle.gif
https://github.com/google/iosched/blob/master/lib/src/main/java/com/google/samples/apps/iosched/feed/FeedViewHolder.java#L51


    private final ConstraintSet expandedConstraints = new ConstraintSet();
    private ConstraintLayout mainLayout;


// 前もってやっておく レイアウトのConstraintSetの内容をcloneする。
expandedConstraints.clone(mainLayout.getContext(), R.layout.feed_message_card_expanded);

// アニメーション + レイアウト変更
TransitionManager.beginDelayedTransition(parent);
expandedConstraints.applyTo(mainLayout);

beginDelayedTransitionapplyToConstraintSet
constraints.clone(context, R.layout.expanded);constrains.applyTo(constraintLayout)

TransitionCustom TransitionTransition

ConstraintLayout使
https://github.com/takahirom/constraint-layout-samples/blob/master/app/src/main/java/com/github/takahirom/constraint_layout_samples/AnimationActivity.kt

ConstraintLayout


BarrierGroupConstraintHelper

ViewView
使
blink.gif

    <com.github.takahirom.constraint_layout_samples.Blink
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="textView,imageView" />

ConstraintHelperを継承したBlinkクラス

class Blink : ConstraintHelper {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun updatePreLayout(container: ConstraintLayout) {
        super.updatePreLayout(container)

        for (i in 0 until this.mCount) {
            val id = this.mIds[i]
            val view = container.getViewById(id)
            if (view != null) {
                ObjectAnimator.ofFloat(view, "alpha", 1F, 0F).apply {
                    repeatCount = ValueAnimator.INFINITE
                    repeatMode = ValueAnimator.REVERSE
                }.start()
            }
        }
    }
}

まとめ

  • いろんなレイアウトの機能が入っていて、
結構いろんなパターンに適応できそう
  • ある程度拡張できるように作ってある
  • ConstraintLayout 1.1が待ち遠しい
  • アニメーションを簡単にできる

参考資料
https://codelabs.developers.google.com/codelabs/constraint-layout/index.html
https://androidstudio.googleblog.com/2017/05/constraintlayout-110-beta-1-release.html
https://medium.com/google-developers/building-interfaces-with-constraintlayout-3958fa38a9f7
https://blog.stylingandroid.com/constraintlayout-chains-spread-chains/
https://www.youtube.com/watch?v=nYb4FUdlLZE
https://academy.realm.io/posts/360-andev-2017-nicolas-roard-advanced-constraintlayout/
https://github.com/googlesamples/android-ConstraintLayoutExamples
https://constraintlayout.com/

354
302
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

354
302