1. 程式人生 > >實現HorizontalScrollView中的標題滑動時始終處於中間位置

實現HorizontalScrollView中的標題滑動時始終處於中間位置

@Override
public void onPageSelected(int position) {
    setCurrentTab(position);
}

實現滑動標題時始終居中的函式

public void setCurrentTab(int position){
    for(int i=0;i<arrTextView.length;i++){
        arrTextView[i].setTextColor(Color.BLACK);
}
    arrTextView[position].setTextColor(Color.RED);
//獲取螢幕寬度
int 
screenWidth=getResources().getDisplayMetrics().widthPixels; //計算控制元件居正中時距離左側螢幕的距離 int middleLeftPosition=(screenWidth-arrTextView[position].getWidth())/2; //正中間位置需要向左偏移的距離 int offset=arrTextView[position].getLeft()-middleLeftPosition; //讓水平的滾動檢視按照執行的x的偏移量進行移動 hs.smoothScrollTo(offset,0); }