編輯:Android編程入門
scrollView 是android系統提供的一種 特殊的展示view。
其實我們很早就遇到過scrollview的東東,比如listview。
而google官方文檔也提出,不要混合使用scrollview & listview,應為他們有一定的沖突性。
本文後面會分析和解決這個問題。
1.認識scrollview
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through. You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView. The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container. ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.
上面這段就是官方描述,大體講了幾個問題。
2.使用scrollview。
官方文檔上,第一行屬性就是android:fillViewport 可見這個屬性對scrollview至關重要!
這是屬性什麼作用:
當你的scrollview的子view,比如說linearlayout,在屏幕夠得情況下,想撐滿整個屏幕,但是你發現無論你怎麼設置layout_height = "match_parent".
它就是不會把整個屏幕撐滿。只會按照wrap_content 的方式顯示。這個就是scrollview特殊的地方。使用android:fillViewport就可以解決這個問題。
3.scrollview & listview
在某些特殊情況下scroollview 和 listview需要同時使用,以下是參考網上的例子寫的一個demo:
布局:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/act_solution_1_sv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0dfeef"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="\nListView上方數據\n" /> <com.joyfulmath.sample.javanetwork.androidbase.scrollview.view.ScrollListView android:id="@+id/act_solution_1_lv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"> </com.joyfulmath.sample.javanetwork.androidbase.scrollview.view.ScrollListView> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="\nListView下方數據\n" /> </LinearLayout> </ScrollView>
import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; /** * @author deman.lu email luyuanchun032@****.com * @version on 2016-03-10 11:12 */ public class ScrollListView extends ListView { public ScrollListView(Context context) { super(context); } public ScrollListView(Context context, AttributeSet attrs) { super(context, attrs); } public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
如此自定義listview以後,listview就可以全部展示在scrollview裡面了。
現在的換膚基本都是切換主題了,畢竟md是主流。先說說切換主題的思路:事先在theme資源文件中准備好各種主流的主題樣式。寫一個工具類,具體功能:sharePreferen
---恢復內容開始---因為android的UI線程是不安全的,如果你UI線程裡執行一些耗時任務,很容易就導致程序崩潰。並且目前網絡任務也不能夠在UI線程裡執行。處理這些
.xml<?xml version=1.0 encoding=utf-8?><LinearLayout xmlns:android=http://sch
有圖又真相,先上圖再說。點擊效果: 設置虛線: [html] view plain copy &