貌似我無法獲取或設置在我簡單的佈局的任何位置或滾動量:獲取孩子的位置,滾動到它
// Relative layout - main_layout
<HorizontalScrollView
android:id="@+id/MenuScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="48dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:padding="0dp" >
<Button
android:id="@+id/button1"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button2"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button3"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button4"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button5"
android:layout_width="150dp"
android:layout_height="fill_parent" />
</LinearLayout>
</HorizontalScrollView>
// End of RelativeLayout - main_layout
我想要什麼,我MainActivity
是得到的位置HorizontalScrollView
中的任何按鈕並滾動到該位置。目前我無法獲得左側值(始終爲0),如果我嘗試使用scrollTo
,HorizontalScrollView
,它就不會,無論x或y的值是多少。
// imports
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
final Button button3 = (Button)findViewById(R.id.button3);
Rect rectf = new Rect();
button3.getLocalVisibleRect(rectf);
Log.i("Left: ", String.valueOf(rectf.left)); // it is always 0
HorizontalScrollView MenuScroll = (HorizontalScrollView)findViewById(R.id.MenuScroll);
// Trying to scroll to X=100 but it doesn't
MenuScroll.scrollTo(100, MenuScroll.getBottom());
}
}
所以,正如我所說,我不能讓左側位置對我的按鈕,或者可以滾動到一些X位置在我HorizontalScrollView
。這可能嗎?
無需遍歷所有孩子的意見,增加了他們的寬度來獲得滾動值。只需設置'scrollTo = button3.getLeft()' – camelCaseCoder 2015-12-17 10:41:49