質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

2回答

2410閲覧

ConstraintLayoutで横幅いっぱい使い1対1のImageViewを均等配置させる方法

momommo

総合スコア12

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2018/06/03 09:45

編集2018/06/03 09:47

比率1対1のImageViewを横幅いっぱい使って、3つ横に並べるよう配置したく、
ConstraintLayoutで以下のようにすれば実現出来るかと思ったのですが、描画されません。
プログラム側で画面幅を取得して、3で割ってwidthを指定しないと実現できないのでしょうか?
よろしくおねがいいたします。

xml

1 <android.support.constraint.ConstraintLayout 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" 4 > 5 6 <TextView 7 android:id="@+id/title" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 app:layout_constraintStart_toStartOf="parent" 11 app:layout_constraintTop_toTopOf="parent" 12 tools:text="タイトル" 13 /> 14 15 <ImageView 16 android:id="@+id/image_view_1" 17 android:layout_width="0dp" 18 android:layout_height="0dp" 19 app:layout_constraintBottom_toBottomOf="parent" 20 app:layout_constraintDimensionRatio="w,1:1" 21 app:layout_constraintEnd_toStartOf="@+id/image_view_2" 22 app:layout_constraintHorizontal_chainStyle="spread_inside" 23 app:layout_constraintHorizontal_weight="1" 24 app:layout_constraintStart_toStartOf="parent" 25 app:layout_constraintTop_toBottomOf="@+id/title" 26 /> 27 28 <ImageView 29 android:id="@+id/image_view_2" 30 android:layout_width="0dp" 31 android:layout_height="0dp" 32 app:layout_constraintBottom_toBottomOf="parent" 33 app:layout_constraintDimensionRatio="w,1:1" 34 app:layout_constraintEnd_toStartOf="@+id/image_view_3" 35 app:layout_constraintHorizontal_weight="1" 36 app:layout_constraintStart_toEndOf="@+id/image_view_1" 37 app:layout_constraintTop_toBottomOf="@+id/title" 38 /> 39 40 <ImageView 41 android:id="@+id/image_view_3" 42 android:layout_width="0dp" 43 android:layout_height="0dp" 44 app:layout_constraintBottom_toBottomOf="parent" 45 app:layout_constraintDimensionRatio="w,1:1" 46 app:layout_constraintEnd_toEndOf="parent" 47 app:layout_constraintHorizontal_weight="1" 48 app:layout_constraintStart_toEndOf="@+id/image_view_2" 49 app:layout_constraintTop_toBottomOf="@+id/title" 50 /> 51 52 </android.support.constraint.ConstraintLayout>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

Spread Chainを使えば等分に配置できます

リンク内容

<?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" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:text="TextView" app:layout_constraintEnd_toStartOf="@+id/imageView" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:src="@drawable/ic_2" android:text="ImageView1" app:layout_constraintEnd_toStartOf="@+id/imageView2" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/textView" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:src="@drawable/ic_2" app:layout_constraintEnd_toStartOf="@+id/imageView3" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:src="@drawable/ic_2" android:text="ImageView3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/imageView2" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>

これは本来デザインを使った方が簡単ですが
デザインの仕様が良く変わるのでわかりにくいかもしれません

全部を掴んでCreate Horizontal ChainがdefaultでSpread Chainになります

投稿2018/06/04 22:28

編集2018/06/04 22:29
aja

総合スコア3733

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

momommo

2018/06/05 05:37

ご回答ありがとうございます! Chain使ってるのですが、比率が1:1のimageviewを並べたいということが実現できなくて困っております。。 heightかwidthのどちらかを設定しなければ実現できないのでしょうか?
aja

2018/06/05 05:41

デザインでまとめて掴んで設定すると それぞれに app:layout_constraintHorizontal_bias="0.5" というのが勝手に設定されますね
momommo

2018/06/05 06:00

はい、質問の方では削除しておりした???? ajaさんが書いてくださったように再度デザインでまとめて掴んで設定し、 app:layout_constraintHorizontal_bias="0.5" がある状態で試してみたのですが、wrap_contentですと、横の均等配置はできますが、 縦が元の画像のmaxの大きさになってしまいます。 Chainとapp:layout_constraintDimensionRatioを使うことで上手いこと出来ないのかなと思ってたのですが。。;
aja

2018/06/05 06:52

>縦が元の画像のmaxの大きさになってしまいます であれば app:layout_constraintDimensionRatio="w,1:1" android:layout_width="0dp" android:layout_height="0dp" あるいは app:layout_constraintDimensionRatio="1" android:layout_width="0dp" android:layout_height="0dp" でどうでしょう
momommo

2018/06/05 07:13

ありがとうございます! 質問の方はこちらで書いているのですが、 これですと0dp/0dpのままで何も描画されない状態です。 > あるいは > app:layout_constraintDimensionRatio="1" こちらも同様でした。
aja

2018/06/05 07:25

あくまで私の例での話です
momommo

2018/06/05 07:32

はい、ajaさんの例でも試しましたが同様でした;
aja

2018/06/05 08:26

呼びだし側のコードが無いので分かりませんが タイミングの問題です レイアウトが生成されるときに画像が無ければ0になります idで画像を張り付ける時には既に遅しです あるいは、再度 setContentView(R.layout.activity_main); のように再描画させてみれば問題かどうかわかると思います
momommo

2018/06/05 10:00

ありがとうございます! > レイアウトが生成されるときに画像が無ければ0になります urlから画像を読み込んでからImageViewに指定してるので、レイアウトが生成されるタイミングでは画像はないですね。。 RecyclerViewの一部の部品として使っていて、 今までは画面の横幅を取得し3で割ったものをwidthに設定してから描画させていたのですが、 ConstraintLayoutのchainとconstraintDimensionRatioを使えばxmlだけで解決できないのかなと思い質問させていただきました。 やはりサイズの設定はこちらでしないと駄目そうですね…
guest

0

layout_constraintWidth_percent33.333... % を指定することで3等分してみました。

xml

1<?xml version="1.0" encoding="utf-8"?> 2<layout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto"> 4 5 <androidx.constraintlayout.widget.ConstraintLayout 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content"> 8 9 <androidx.appcompat.widget.AppCompatImageView 10 android:id="@+id/start_image_view" 11 android:layout_width="0dp" 12 android:layout_height="0dp" 13 android:src="@mipmap/ic_launcher_round" 14 app:layout_constraintDimensionRatio="1:1" 15 app:layout_constraintEnd_toStartOf="@+id/center_image_view" 16 app:layout_constraintStart_toStartOf="parent" 17 app:layout_constraintWidth_percent="33.333333" /> 18 19 <androidx.appcompat.widget.AppCompatImageView 20 android:id="@+id/center_image_view" 21 android:layout_width="0dp" 22 android:layout_height="0dp" 23 android:src="@mipmap/ic_launcher_round" 24 app:layout_constraintDimensionRatio="1:1" 25 app:layout_constraintEnd_toStartOf="@+id/end_image_view" 26 app:layout_constraintStart_toEndOf="@id/start_image_view" 27 app:layout_constraintWidth_percent="33.333333" /> 28 29 <androidx.appcompat.widget.AppCompatImageView 30 android:id="@+id/end_image_view" 31 android:layout_width="0dp" 32 android:layout_height="0dp" 33 android:src="@mipmap/ic_launcher_round" 34 app:layout_constraintDimensionRatio="1:1" 35 app:layout_constraintEnd_toEndOf="parent" 36 app:layout_constraintStart_toEndOf="@id/center_image_view" 37 app:layout_constraintWidth_percent="33.333333" /> 38 </androidx.constraintlayout.widget.ConstraintLayout> 39</layout>

????

投稿2018/10/02 10:44

84d010m08

総合スコア58

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問