HOME技術AndroidAndroidアプリ:エラー「ScrollView can host only one direct child」

Androidアプリ:エラー「ScrollView can host only one direct child」

Android StudioでScrollViewを設定して動作確認しようとすると、「ScrollView can host only one direct child」というエラーが出て動作確認できないことがあります。

ScrollViewは子要素を1つしか持てない

ScrollView内には子要素を1つしか持てないため、複数の子要素を指定している場合にこのエラーが出ます。

ScrollView内に子要素を複数持たせたい場合には、LinearLayoutなどで全体を囲ってから、その内部で子要素を複数配置します。

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content">
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content">
/>

</LinearLayout>

</LinearLayout>
</ScrollView>

上記のようにすればエラーは出ません。

関連記事

Androidアプリ:When using intent filters, please specify ‘android:exported’ as well

Android Studioで「When using intent filters, please specify ‘android:exported’ as well」という…続きを読む

Androidアプリ:ERROR: This project uses AndroidX dependencies, but the ‘android.useAndroidX’ property is not enabled.

Android Studioでビルドしようとしたら、「ERROR: This project uses AndroidX dependencies, but the ‘android.us…続きを読む

Androidアプリ:Class ‘Anonymous class derived from OnItemClickListener’…のエラー

Android Studioで開発中に、ListViewを使っていて以下のようなエラーメッセージが出た場合の理由です。 Class ‘Anonymous class derived from OnIt…続きを読む

Androidアプリ:ボタンのテキストを変える

Androidでボタンのテキストをプログラム上で変える方法です。 MainActivity.java   Button button = findViewById(R.id.button);…続きを読む

Androidアプリ:フォーカスをあてたEditTextのテキストを全選択する

Android StudioでEditTextにフォーカスが当たったと同時に、既に入力されている文字列をすべて選択した状態にする方法です。 EditTextタグに以下のコードを追加 android:s…続きを読む

Androidアプリ:特定のViewに最初のフォーカスをあてる

Android Studioにおいて、起動時に特定のViewにフォーカスをあてる方法です。 activity_main.xmlで設定 レイアウト定義ファイル(activity_main.xml)におい…続きを読む

Androidアプリ:ボタンやEditTextを角丸にする

Android StudioでボタンやEditTextを配置して、角丸にします。 角丸や色などを指定する場合には、それ専用のxmlファイルを別途用意し、読み込ませるようにします。 ボタンの背景色と角丸…続きを読む

Androidアプリ:ポリシー違反の警告への対応(APIレベル)

Google Play Console上で、アプリにポリシー違反の警告が表示されていました。 「お客様のアプリはGoogle Playデベロッパープログラムポリシーを遵守していないため、まもなく措置を…続きを読む

Androidアプリ:ボタンやEditTextに背景色・枠線をつける

Android Studioで、ボタンやEditTextに背景色や枠線をつける方法です。 コントロールの見た目の設定をするには、別途専用のxmlファイルを生成して、そこに書いた設定を読み込むようにしま…続きを読む

Androidアプリを実機で確認する

AndroidアプリをAndroid Studioで開発した際に、実際のスマホの画面で動作確認する方法です。 スマホの設定 以下はArrows FX F-02Hの設定方法です(開発者向けオプションを表…続きを読む