當(dāng)內(nèi)容過大屏幕無法顯示完全時(shí),可以使用ScrollView來擴(kuò)大器顯示區(qū)域?!臼褂孟拗啤?div style="height:15px;">
1.ScrollView是Framelayout的一種特殊形態(tài)。ScrollView只能包含一個(gè)View或一個(gè)ViewGroup。并且其布局必須是LinearLayout.
2.不能將ScrollView和ListView混在一起用,因?yàn)锳ndroid本身已針對ListView作了適應(yīng)大規(guī)模數(shù)據(jù)的顯示處理。
在ScrollView添加一個(gè)ListView造成的滾動(dòng)問題的簡單解決辦法文章分類:
移動(dòng)開發(fā)正常來說,在ScrollView添加一個(gè)ListView后在真機(jī)上只會(huì)顯示ListView的一行多一點(diǎn),我也不理解為什么會(huì)這樣,后來我把ListView的layout_height改成400dip,而不是用match_parent和wrap_content,我發(fā)現(xiàn)這樣的話ListView就顯示的多了很多。所以就產(chǎn)生了把ListView所有的item的高度算出來給ListView設(shè)置的想法。下面是代碼:
Java代碼
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
params.height += 5;//if without this statement,the listview will be a little short
listView.setLayoutParams(params);
}
設(shè)置listView高度的另一種寫法(因?yàn)橛袝r(shí)上面的那種寫法會(huì)報(bào)類轉(zhuǎn)換異常):
LayoutParams params = (LayoutParams) getListView().getLayoutParams();
params.height = height;
params.width = LayoutParams.FILL_PARENT;
getListView().setLayoutParams(params);
在代碼的倒數(shù)第二行二我又給加了5個(gè)像素,這是因?yàn)槲以趌istview的屬性里面添加了padding=5dip。
然后每次ListView的數(shù)據(jù)一有變化就用這個(gè)函數(shù)設(shè)置一下就好了,不過這樣總感覺效率很低,希望有達(dá)人給指點(diǎn)一下。
簡單來說就是把layout_height寫死,這種辦法也很適用于GridView(如果能估計(jì)得出GridView的高度的話)。
listview與ScrollView老問題的另類解法
http://www.eoeandroid.com/thread-42893-1-1.html這幾天一直被listview怎么合理的放進(jìn)scorllview中的問題困擾,嘗試過把listview放入scorllview中的朋友都知道,被放入的listview顯示是有問題的,無論怎么設(shè)置layout都只顯示大概2行的高度,看起來很郁悶,更別說美觀了,后來上網(wǎng)查詢了一下,解決方法有的是用linearlayout替換listview,還有修改onmeasure的,我比較懶個(gè)人感覺很麻煩不喜歡,終于想出了一個(gè)還算和諧的解決方法:xml中的textlist設(shè)置如下:
<?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="wrap_content"
android:orientation="vertical"
android:background="#44444444">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:background="#ff888888">
<TextView
android:text="あ"
android:textColor="#ffeeeeee"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<ListView
android:scrollbars="none"
android:stackFromBottom="true"
android:id="@+id/lv0"
android:layout_width="fill_parent"
android:layout_height="20dp"></ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
其中的textview是我做的東西要用到的,和方法無關(guān)可以不看,然后就是在java中重新設(shè)置listview的高度了,目的是把listview“撐”開:
LinearLayout.LayoutParams lp5 =new LinearLayout.LayoutParam(LayoutParams.FILL_PARENT, listItem.size()*51-1);
其中第一個(gè)屬性不必說了,第二個(gè)是為了計(jì)算listview要設(shè)置的總高度用的,51是我事先設(shè)置好的一行的高度(50)+每行之間的間隔(1)而得來的,listItem.size()是我要顯示的行數(shù),用.setLayoutParams(lp5);來重新設(shè)置高度,其他別的設(shè)置跟以前一樣,想要源碼我整理完之后貼出來
如果不想寫死,那就看下面的文章
Android 解決ListView 和 ScrollView 共存沖突的問題
http://labs.chinamobile.com/mblog/532767_72693?wralxianxrnxhttp://blog.liaoxiaoqi.com/?p=503