Получить дочернее местоположение в HorizontalScrollView и выделите его
Похоже, я не могу получить или установить какую-либо позицию или сумму прокрутки в моем простом макете:
// 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
, Это возможно?