简体   繁体   English

如何从Horizo​​ntalScrollView中获取屏幕宽度

[英]How to get screen width from within a HorizontalScrollView

I want to create a view which contains several elements and which can get wider than the screen. 我想创建一个包含几个元素的视图,它可以变得比屏幕更宽。 Therefore I put it in a HorizontalScrollView : 因此,我将其放在HorizontalScrollView

<HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:id="@+id/horizontalScrollView" >

<view class="com.hb.examples.myapplication2.app.ui.controls.LineGraphView"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:id="@+id/line_chart"/>

</HorizontalScrollView>

The containing View can get wider, but don't have to. 包含的View 可以扩大,但不必这样做。 When its smaller than the screen I want to be it as wide as possible without displaying the Scrollbar. 当它小于屏幕时,我希望它尽可能宽而不显示滚动条。 So how do I get the Screenwidth to return in to Android via setMeasuredDimension in onMeasure ? 那么,如何通过onMeasure setMeasuredDimension获取Screenwidth以返回Android?

Since you're in a view, you can retrieve display metrics from the context the view is associated with 由于您处于视图中,因此可以从与视图关联的上下文中检索显示指标

WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
setMeasuredDimension(metrics.widthPixels, MeasureSpec.getSize(heightMeasureSpec));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM