我正在开发一个由水平滚动视图组成的应用程序,我想获得 HorizontalScrollView 的宽度。我能实现这个目标吗?它对我来说是零回归
XMl中的
<TextView
android:layout_width="50dp"
android:layout_height="10dp"
android:background="#00ff00" />
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#ff0000" >
<TextView
android:layout_width="50dp"
android:layout_height="10dp"
android:background="#00ff00" />
</HorizontalScrollView>
在Java中
public class TestApplication extends Activity{
private HorizontalScrollView horizontalScrollView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testscroll);
horizontalScrollView1 = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
horizontalScrollView1.getRight();
horizontalScrollView1.getWidth();
System.out.println("horizontalScrollView1.getWidth();::>"+horizontalScrollView1.getWidth());
System.out.println("horizontalScrollView1.getWidth();::1>"+horizontalScrollView1.getMeasuredWidth());
}
我试过以下方法可以帮助我。
由于 NIKHIL
答案 0 :(得分:2)
你可以这样做:
int width = 0;
ViewTreeObserver vto = horizontalScrollView1.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
width = horizontalScrollView1.getWidth();
}
});
希望能提供帮助:)
答案 1 :(得分:1)
请尝试这样
Log.d("TAG","scrollbar width: " + scrollView.getChildAt(0).getWidth());
Log.d("TAG","scrollbar height: " + scrollView.getChildAt(0).getHeight());
答案 2 :(得分:0)
而不是getWidth()
方法尝试使用getMeasuredWidth()
,它会帮助您,我想
答案 3 :(得分:0)
您可以在这种情况下实现:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int width = horizontalScrollView1.getMeasuredWidth();
int height = horizontalScrollView1.getMeasuredHeight();
System.out.println("widrh::>"+width);
System.out.println("height::>"+height);
}