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

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

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

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

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1133閲覧

LinearLayoutでボタンを等間隔で横に並べる

kmhong

総合スコア5

Android

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

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2023/05/31 10:57

実現したいこと

LinearLayoutでボタンを等間隔で横に並べるには、どのようにすればよいでしょうか?

試したこと

全体的な配置は縦に並べたかったので、ルートはLinearLayoutのverticalに設定しました。
その並びの中で、3つのボタンを横に等間隔に並べたかったので、入れ子でhorizontalのLinearLayoutを作成し、
その中にボタンを3つ配置しました。
検索すると「layout_width=”0dp”」と「layout_weight=”1″」にするという方法が見つかるのですが、実際にそのように組んでみても、等間隔に離れませんでした。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:gravity="center"> <TextView (省略) android:layout_marginTop="60dp" /> <TextView (省略) android:layout_marginTop="60dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="50dp"> <Button android:id="@+id/button4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> </LinearLayout> </LinearLayout>

イメージ説明

補足情報(FW/ツールのバージョンなど)

Android Studio Flamingo | 2022.2.1 Patch 2

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

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

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

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

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

jimbe

2023/05/31 11:03

LinearLayout じゃないといけないのでしょうか。 ボタンのテキスト(の長さ)が変わったらどのような感じになるべきなのでしょうか。
guest

回答1

0

ベストアンサー

まず、ボタンの LinearLayout は android:layout_width="wrap_content" なのでボタンの幅の合計になりますので、隙間は空きようがありません。

xml

1 <LinearLayout 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" 4 android:orientation="horizontal"> 5 <Space 6 android:layout_width="0dp" 7 android:layout_height="0dp" 8 android:layout_weight="1" /> 9 <Button 10 android:id="@+id/button4" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Button" /> 14 <Space 15 android:layout_width="0dp" 16 android:layout_height="0dp" 17 android:layout_weight="1" /> 18 <Button 19 android:id="@+id/button5" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="Button" /> 23 <Space 24 android:layout_width="0dp" 25 android:layout_height="0dp" 26 android:layout_weight="1" /> 27 <Button 28 android:id="@+id/button6" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="Button" /> 32 <Space 33 android:layout_width="0dp" 34 android:layout_height="0dp" 35 android:layout_weight="1" /> 36 </LinearLayout>

投稿2023/05/31 11:06

編集2023/05/31 11:21
jimbe

総合スコア12632

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

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

kmhong

2023/05/31 11:56

ご回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問