首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取HorizontalScrollView宽度

如何获取HorizontalScrollView宽度
EN

Stack Overflow用户
提问于 2012-08-04 20:37:09
回答 4查看 4K关注 0票数 1

我正在开发一个由水平滚动视图组成的应用程序,我想获得HorizontalScrollView.How的宽度,我可以做到这一点?对我来说,它返回为零

在XMl中有这样的。

代码语言:javascript
复制
<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中

代码语言:javascript
复制
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

EN

回答 4

Stack Overflow用户

发布于 2012-08-04 20:40:29

你可以这样做:

代码语言:javascript
复制
int width = 0;
ViewTreeObserver vto = horizontalScrollView1.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        width = horizontalScrollView1.getWidth();
    }
});

希望能帮上忙:)

票数 2
EN

Stack Overflow用户

发布于 2018-07-09 01:58:21

请这样试一试

代码语言:javascript
复制
Log.d("TAG","scrollbar width: " + scrollView.getChildAt(0).getWidth());
Log.d("TAG","scrollbar height: " + scrollView.getChildAt(0).getHeight());
票数 1
EN

Stack Overflow用户

发布于 2012-08-04 20:53:02

我想用getMeasuredWidth()代替getWidth()方法会对你有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11808746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档