#功能:当横向滚动条滚到顶端时,左箭头灰掉;当滚到尾端时,右箭头灰掉;当滚到中间时,左右箭头水红色;
效果如下图
<!-- /res/layout/horizontal_view_demo.xml --> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="40dp"> <TextView android:id="@+id/h_left" android:layout_width="13dp" android:layout_height="fill_parent" android:textSize="20dp" android:gravity="center" android:background="@color/grey" android:text="<"/> <HorizontalScrollView android:id="@+id/h_horizontal" android:layout_width="200dp" android:layout_height="fill_parent" android:layout_weight="1" android:scrollbars="none"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/h_t1" android:layout_width="70dp" android:layout_height="fill_parent" android:textSize="20dp" android:gravity="center" android:layout_margin="1dp" android:background="#666666" android:text="人事部"/> <TextView android:id="@+id/h_t2" android:layout_width="70dp" android:layout_height="fill_parent" android:textSize="20dp" android:gravity="center" android:layout_margin="1dp" android:background="#666666" android:text="账务部"/> <TextView android:id="@+id/h_t3" android:layout_width="70dp" android:layout_height="fill_parent" android:textSize="20dp" android:gravity="center" android:layout_margin="1dp" android:background="#666666" android:text="技术部"/> <TextView android:id="@+id/h_t4" android:layout_width="70dp" android:layout_height="fill_parent" android:textSize="20dp" android:gravity="center" android:layout_margin="1dp" android:background="#666666" android:text="销售部"/> <TextView android:id="@+id/h_t5" android:layout_width="70dp" android:layout_height="fill_parent" android:textSize="20dp" android:gravity="center" android:layout_margin="1dp" android:background="#666666" android:text="制作部"/> <TextView android:id="@+id/h_t6" android:layout_width="70dp" android:layout_height="fill_parent" android:textSize="20dp" android:gravity="center" android:layout_margin="1dp" android:background="#666666" android:text="设计部"/> <TextView android:id="@+id/h_t7" android:layout_width="70dp" android:layout_height="fill_parent" android:textSize="20dp" android:gravity="center" android:layout_margin="1dp" android:background="#666666" android:text="行政部"/> </LinearLayout> </HorizontalScrollView> <TextView android:id="@+id/h_right" android:layout_width="13dp" android:layout_height="fill_parent" android:textSize="20dp" android:gravity="center" android:background="@color/pink" android:text=">"/> </LinearLayout> </LinearLayout>
public class HorizontalViewDemo extends Activity { private HorizontalScrollView hsv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.horizontal_view_demo); hsv = (HorizontalScrollView)findViewById(R.id.h_horizontal); hsv.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View view, MotionEvent me) { TextView hLeft = (TextView)findViewById(R.id.h_left); TextView hRight = (TextView)findViewById(R.id.h_right); if (hsv.getScrollX() == 0) { hLeft.setBackgroundResource(R.color.grey); } else if (hsv.getScrollX() > 0) { hLeft.setBackgroundResource(R.color.pink); } if (hsv.getScrollX() == 210) {//210这个值在不同像数需要改变 hRight.setBackgroundResource(R.color.grey); } else if (hsv.getScrollX() < 210) { hRight.setBackgroundResource(R.color.pink); } return false; } }); } }