Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 确定视图的宽度和高度_Android - Fatal编程技术网

Android 确定视图的宽度和高度

Android 确定视图的宽度和高度,android,Android,我是Android编程新手 我的问题是,确定子视图的宽度和高度的最佳方法是什么 我正在写一个应用程序,其中包括一个钢琴键盘输入 我有一个自定义视图,PianoKeyboardView。 我需要知道视图的尺寸才能绘制键盘。 如果我将下面代码中的宽度和高度填充到我的钢琴键盘视图中,我的钢琴键盘画得很好 Display display = ((WindowManager) this .getSystemService(Context.WINDOW_SERVICE)).getDefau

我是Android编程新手

我的问题是,确定子视图的宽度和高度的最佳方法是什么

我正在写一个应用程序,其中包括一个钢琴键盘输入

我有一个自定义视图,PianoKeyboardView。 我需要知道视图的尺寸才能绘制键盘。 如果我将下面代码中的宽度和高度填充到我的钢琴键盘视图中,我的钢琴键盘画得很好

Display display = ((WindowManager) this 
        .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    int width = display.getWidht();
    int height = display.getHeight();
显然我不想这样做

我在Eclipse中创建了一个默认的android应用程序,并选择了全屏选项。onCreate的默认代码为:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);
在默认视图上调用getWidth()和getHeight()时,得到0

    int width = controlsView.getWidth();
    int height = controlsView.getHeight();
    width = contentView.getWidth();
    height = contentView.getHeight();
我的PianoKeyboardView的宽度和高度也是0,这就是我的问题

My activity_fullscreen.xml将所有视图的宽度和高度设置为“匹配父视图”

<FrameLayout    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <LinearLayout
        android:id="@+id/fullscreen_content_controls"
        style="?buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/black_overlay"
        android:orientation="horizontal"
        tools:ignore="UselessParent" >

        <com.application.piano.PianoKeyboardView
        android:id="@+id/keyboard_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             android:layout_weight="1"
        />

谢谢

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

    ViewTreeObserver viewTreeObserver = controlsView.getViewTreeObserver();
    viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int width = controlsView.getWidth();
                int height = controlsView.getHeight();
                return true;
            }
    });

希望这对您有所帮助….

您可以在此基础上尝试一些东西。。。(抄袭自)

我使用以下技术-发布一个runnable from
onCreate()
,创建视图时将执行该操作:

    contentView = findViewById(android.R.id.content);
    contentView.post(new Runnable()
    {
        public void run()
        {
            contentHeight = contentView.getHeight();
        }
    });

onCreate()
完成后,此代码将在主UI线程上运行。在这一点上,视图有了一个大小。

非常感谢!是的,当在PianoKeyboardView中调用onDraw()方法时,宽度和高度已经初始化。所以我只是延迟初始化键盘,直到第一次调用onDraw。