1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ConstraintLayout でViewを移動する方法

Posted at

ConstraintLayout でViewを移動するにはConstraintSetを使用し、制約を編集する必要があります。
このサンプルではUPボタンを押せば上へDOWNボタンを押せば下へTextViewが移動します。

#コード

activity_main.xml
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="up"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnDwon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="down"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnUp" />

</android.support.constraint.ConstraintLayout>
package com.example.test;

import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTextView;
    private Button mBtnUp;
    private Button mBtnDown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = findViewById(R.id.textView);
        mBtnUp = findViewById(R.id.btnUp);
        mBtnDown = findViewById(R.id.btnDwon);

        mBtnUp.setOnClickListener(this);
        mBtnDown.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int newMergin;
        // 現在のLayoutParamを取得
        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mTextView.getLayoutParams();
        if (view.equals(mBtnUp)) {
            newMergin = lp.topMargin - 32; // Topマージンを-32する
        } else if (view.equals(mBtnDown)) {
            newMergin = lp.topMargin + 32; // Topマージンを+32する
        } else {
            return;
        }

        // ConstraintLayoutを取得
        ConstraintLayout cl = findViewById(R.id.root);
        ConstraintSet constraintSet = new ConstraintSet();
        // 既存のConstraintLayoutの設定をクローンする
        constraintSet.clone(cl);
        // 制約を設定する
        constraintSet.connect(R.id.textView, ConstraintSet.TOP,
                ConstraintSet.PARENT_ID, ConstraintSet.TOP, newMergin);
        // 設定を反映する
        constraintSet.applyTo(cl);
    }
}

#参考

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?