我正在开发一个由水平滚动视图组成的应用程序,我想获得HorizontalScrollView.How的宽度,我可以做到这一点?对我来说,它返回为零
在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
发布于 2012-08-04 20:40:29
你可以这样做:
int width = 0;
ViewTreeObserver vto = horizontalScrollView1.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
width = horizontalScrollView1.getWidth();
}
});希望能帮上忙:)
发布于 2018-07-09 01:58:21
请这样试一试
Log.d("TAG","scrollbar width: " + scrollView.getChildAt(0).getWidth());
Log.d("TAG","scrollbar height: " + scrollView.getChildAt(0).getHeight());发布于 2012-08-04 20:53:02
我想用getMeasuredWidth()代替getWidth()方法会对你有帮助
https://stackoverflow.com/questions/11808746
复制相似问题