相关疑难解决方法(0)

在运行时确定Android视图的大小

在创建活动后,我正在尝试将动画应用到我的Android应用中的视图.为此,我需要确定视图的当前大小,然后设置动画以从当前大小缩放到新大小.此部分必须在运行时完成,因为视图根据用户的输入缩放到不同的大小.我的布局是用XML定义的.

这似乎是一件容易的事,虽然没有一个能解决我的问题,但很多问题都存在.所以也许我错过了一些明显的东西.我通过以下方式了解了我的观点:

ImageView myView = (ImageView)getWindow().findViewById(R.id.MyViewID);
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但打电话时getWidth(),getHeight(),getMeasuredWidth(),getLayoutParams().width,等,他们都返回0.我也试过手动调用measure()视图上,然后到一个呼叫getMeasuredWidth(),但没有效果.

我已经尝试调用这些方法并在我的activity onCreate()和in中检查调试器中的对象onPostCreate().如何在运行时确定此视图的确切尺寸?

android

113
推荐指数
8
解决办法
14万
查看次数

如何获得LinearLayout的高度

我有一个LinearLayout设置高度为match_parent,如下所示:

<LinearLayout
    android:id="@+id/list_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
Run Code Online (Sandbox Code Playgroud)

我想得到这个LinearLayout的高度.
我使用下面的代码:

LinearLayout ll_list = (LinearLayout)findViewById(R.id.list_layout);
int h = ll_list.getHeight();
Run Code Online (Sandbox Code Playgroud)

但它返回null.
我能怎么做?

android android-linearlayout

12
推荐指数
1
解决办法
2万
查看次数

布局的getHeight()由ViewTreeObserver返回零

我在OnCreate方法中使用ViewTreeObserver来获取我的工具栏和底部布局的高度,但我仍然得到0高度,为什么?难道我做错了什么?

这是我打电话的方式:

ViewTreeObserver viewTreeObserver = toolbar.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                // Ensure you call it only once :
                toolbar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                height1 = toolbar.getMeasuredHeight();
            }
        });

        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.bottom);
        ViewTreeObserver vto = linearLayout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                // Ensure you call it only once :
                linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                height2 = linearLayout.getMeasuredHeight();

            }
        });

        Toast.makeText(getApplicationContext(), String.valueOf(height1) + String.valueOf(height2), Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)

android

2
推荐指数
1
解决办法
2189
查看次数

标签 统计

android ×3

android-linearlayout ×1