2011-10-25 30 views

回答

5

這將涉及一些計算。

按照樣式,您需要滾動到位置以居中元素。

int scrollX = (button.getLeft() - (screenWidth/2))+(button.getWidth()/2); 
hScrollView.scrollTo(scrollX,0); 

button -> It is the button you are trying to center. 

screenWidth -> Width of the screen. 
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); 

hScrollView -> It is the horizontal scrollview. 

但是,即使使用這種方法,你就不能到中心端部件如scrollviews不會滾動超越其邊界。

+0

我想你再問一件事? –

+0

假設我有10個選項卡時,如1,2,3,4點擊任何選項卡時,它應該被替換在位置2 .. –

0
public void onTabChanged(String tabId) { 
    for (int i = 0; i < tablist.length; i++) { 
     if (tabId.contentEquals(tablist[i])) { 
      HorizontalScrollView scroll = (HorizontalScrollView)findViewById(R.id.horScrollView); 
      scroll.smoothScrollTo(((int)(getResources().getDimensionPixelSize(R.dimen.tab_width) * (i))), 0); 
     } 
    } 

我用過:實現OnTabChangeListener。