相关疑难解决方法(0)

应用程序启动时读取布局大小

我需要ScrollView在活动启动期间检索布局xml文件中定义的高度.哪个是实现此目的的最佳实践.我试过把代码放在里面onCreate(),onStart()然后onResume().All在启动时将高度设为0.是否有像活动一样onFinishInflate()的方法?

这是我的代码:

myScroll=(ScrollView) findViewById(R.id.ScrollView01);
int height=myScroll.getHeight();
Run Code Online (Sandbox Code Playgroud)

android android-layout android-scrollview

5
推荐指数
1
解决办法
373
查看次数

在显示之前计算视图测量

我试图尽可能动态地向LinearLayout添加视图(取决于屏幕宽度).

我在屏幕上显示LinearLayout之前执行此操作.

我的LinearLayout:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" 
    android:background="#666"/>
Run Code Online (Sandbox Code Playgroud)

我在LinearLayout中显示的视图:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp"
    android:background="#999">
    <ImageView
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/no_photo"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我添加了布局视图:

int allItems = 50;
int currentItem = 0;
while(currentItem < allItems)
{
    FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fl, null);

    linearLayout.addView(view);

    if (linearLayout.getMeasuredWidth() >= this.getWidth())
    {
        linearLayout.removeView(view);
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

但linearLayout.getMeasuredWidth()和this.getWidth()为0;

我知道,我必须使用View.measure方法计算视图大小才可见,但我不知道它在何处以及如何使用.

size android view measure

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

获取设置为包装内容的布局高度

我想获得设置为包装内容的线性布局的高度。但它是零。我试试这个代码:

 final LinearLayout below= (LinearLayout) findViewById(R.id.below);


         ViewTreeObserver vto = below.getViewTreeObserver();  
         vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
             @Override  
             public void onGlobalLayout() {  
                  width  = below.getMeasuredWidth(); 
                  height = below.getMeasuredHeight();  

             }  
         });
Run Code Online (Sandbox Code Playgroud)

和 :

 LinearLayout below= (LinearLayout) findViewById(R.id.below);
       final int belowh=below.getHeight();
Run Code Online (Sandbox Code Playgroud)

使用代码:

 final LinearLayout below= (LinearLayout) findViewById(R.id.below);
        ViewTreeObserver vto = below.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @Override 
            public void onGlobalLayout() { 

        if(below.getMeasuredHeight()> 0){

                 this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                int width  = below.getMeasuredWidth();
                int height = below.getMeasuredHeight(); 
        }
            } 
        })

    ;
Run Code Online (Sandbox Code Playgroud)

点击这里查看

android android-linearlayout

3
推荐指数
1
解决办法
4441
查看次数

如何知道活动何时布局?

我有一个mapview,我想把一些标记放在上面.当我开始活动时,我将从web服务中检索这些,因此我需要知道当前视口的最小和最大lat/lng对.我在打电话

mMapView.getWidth()
mMapView.getHeight()
Run Code Online (Sandbox Code Playgroud)

但是在活动开始时他们都返回0.我尝试将它放在onAttachedToWindow,onResume,onPostCreate,onPostResume,onStart等等,但无济于事.我怎么知道活动已完成所有视图的布局并准备好给我正确的高度和宽度测量值?

layout android android-mapview

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