【问题标题】:Determining the width and height of a View确定视图的宽度和高度
【发布时间】:2014-01-29 17:30:10
【问题描述】:

我是 Android 编程新手。

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

我正在编写一个包含用于输入的钢琴键盘的应用程序。

我有一个自定义视图,PianoKeyboardView。 我需要知道 View 的尺寸才能绘制键盘。 如果我将以下代码中的宽度和高度填充到我的 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,这是我的问题。

我的 activity_fullscreen.xml 将所有视图的宽度和高度设置为“match_parent”

<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"
        />

谢谢。

【问题讨论】:

    标签: android


    【解决方案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;
                }
        });
    

    希望对你有所帮助.....

    【讨论】:

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

    您可以基于此尝试一些东西...(复制自my answer to a related question)。

    我使用以下技术 - 从 onCreate() 发布一个可运行的,将在创建视图时执行:

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

    此代码将在onCreate() 完成后在主 UI 线程上运行。此时视图已经有了大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-31
      • 2011-08-21
      • 2012-02-23
      • 2017-11-23
      • 1970-01-01
      • 2013-02-21
      • 2013-01-02
      • 1970-01-01
      相关资源
      最近更新 更多