Android ScrollView只能添加一個子控件問題解決方法
本文實例講述了Android ScrollView只能添加一個子控件問題解決方法。分享給大家供大家參考,具體如下:
有下面一段代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
一個ScrollView里面添加了三個Button,也許你認為沒有什么問題,那么我們運行一下看看
出現(xiàn)了一個異常

很明顯,異常告訴我們ScrollView can host only one direct child
既然說只能容納一個直接的子控件,那么我們就可以容納多個間接的子控件,直接在這些子控件外面再套一層LinearLayout就OK了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android中使用SeekBar拖動條實現(xiàn)改變圖片透明度(代碼實現(xiàn))
這篇文章主要介紹了Android中使用SeekBar拖動條實現(xiàn)改變圖片透明度,需要的朋友可以參考下2020-01-01
Android開發(fā)技巧之像QQ一樣輸入文字和表情圖像
QQ聊天輸入框,在輸入框中可以同時輸入文字和表情圖像。實際上,這種效果在Android SDK中只需要幾行代碼就可以實現(xiàn),本文將會介紹如何實現(xiàn)像QQ一樣輸入表情圖像2013-01-01
Android FlowLayout流式布局實現(xiàn)詳解
這篇文章主要為大家詳細介紹了Android FlowLayout流式布局的實現(xiàn)方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09
Android 游戲引擎libgdx 資源加載進度百分比顯示案例分析
因為案例比較簡單,所以簡單用AndroidApplication -> Game -> Stage 搭建框架感興趣的朋友可以參考下2013-01-01
Android使用fragment實現(xiàn)左側(cè)導(dǎo)航
這篇文章主要為大家詳細介紹了Android使用fragment實現(xiàn)左側(cè)導(dǎo)航,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
Android碎片fragment實現(xiàn)靜態(tài)加載的實例代碼
這篇文章主要介紹了Android碎片fragment實現(xiàn)靜態(tài)加載的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11

