ScrollView和HorizontalScrollView常用屬性,及禁止滑動
阿新 • • 發佈:2019-01-04
常用屬性:詳見註釋
activity_main_28
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width ="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fadingEdge="none"
android:overScrollMode="never"
android:scrollbarThumbVertical="@drawable/bar_style_v"
>
<!--ScrollView滾動條不顯示 android:scrollbars="none"-->
<!--ScrollView滾動條恆顯示:android:fadeScrollbars="false"-->
<!--設定垂直滾動條的drawable(如顏色):android:scrollbarThumbVertical,如果ScrollView中使用android:scrollbarThumbHorizontal,沒有效果。反之亦然-->
<!--設定滾動條的大小:android:scrollbarSize="20dp" ScrollView中,代表寬度-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height ="match_parent"
android:orientation="vertical">
<TextView
style="@style/TV"
android:text="111"
/>
<TextView
style="@style/TV"
android:text="222"
/>
<TextView
style="@style/TV"
android:text="333"
/>
<TextView
style="@style/TV"
android:text="444"
/>
<TextView
style="@style/TV"
android:text="555"
/>
<TextView
style="@style/TV"
android:text="666"
/>
<TextView
style="@style/TV"
android:text="777"
/>
<TextView
style="@style/TV"
android:text="888"
/>
<TextView
style="@style/TV"
android:text="999"
/>
<TextView
style="@style/TV"
android:text="11111"
/>
<TextView
style="@style/TV"
android:text="22222"
/>
<TextView
style="@style/TV"
android:text="33333"
/>
<TextView
style="@style/TV"
android:text="44444"
/>
<TextView
style="@style/TV"
android:text="55555"
/>
<TextView
style="@style/TV"
android:text="66666"
/>
<TextView
style="@style/TV"
android:text="77777"
/>
<TextView
style="@style/TV"
android:text="88888"
/>
<TextView
style="@style/TV"
android:text="99999"
/>
</LinearLayout>
</ScrollView>
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#0000ff"/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
android:overScrollMode="never"
android:scrollbarThumbHorizontal="@drawable/bar_style_h"
>
<!--滾動條不顯示 android:scrollbars="none"-->
<!--滾動條恆顯示:android:fadeScrollbars="false"-->
<!--設定水平滾動條的drawable(如顏色):android:scrollbarThumbHorizontal-->
<!--設定滾動條的大小:android:scrollbarSize="20dp" HorizontalScrollView中,代表高度-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="@style/TV"
android:text="111"
/>
<TextView
style="@style/TV"
android:text="222"
/>
<TextView
style="@style/TV"
android:text="333"
/>
<TextView
style="@style/TV"
android:text="444"
/>
<TextView
style="@style/TV"
android:text="555"
/>
<TextView
style="@style/TV"
android:text="666"
/>
<TextView
style="@style/TV"
android:text="777"
/>
<TextView
style="@style/TV"
android:text="888"
/>
<TextView
style="@style/TV"
android:text="999"
/>
<TextView
style="@style/TV"
android:text="11111"
/>
<TextView
style="@style/TV"
android:text="22222"
/>
<TextView
style="@style/TV"
android:text="33333"
/>
<TextView
style="@style/TV"
android:text="44444"
/>
<TextView
style="@style/TV"
android:text="55555"
/>
<TextView
style="@style/TV"
android:text="66666"
/>
<TextView
style="@style/TV"
android:text="77777"
/>
<TextView
style="@style/TV"
android:text="88888"
/>
<TextView
style="@style/TV"
android:text="99999"
/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
bar_style_v和bar_style_h內容一樣:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000"/>
</shape>
只是單純展示,主activity不必做任何操作
/**
* ScrollView
* HorizontalScrollView
*/
public class MainActivity_28_ScrollView_HorizontalScrollView extends BaseActivity {
@Override
void initview() {
setContentView(R.layout.activity_main_28);
}
}
禁止滑動:
scroll_view = (ScrollView) findViewById(R.id.scroll_view);
scroll_view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
Log.e("chen", "ScrollView-onTouch");
//不能滑動
return true;
//可以滑動
//return false;
}
});
horizontal_scroll_view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
Log.e("chen", "HorizontalScrollView-onTouch");
//不能滑動
return true;
//可以滑動
//return false;
}
});