的getHeight返回0,所有的Android UI对象 [英] getHeight returns 0 for all Android UI objects
问题描述
我要建一个用户界面,它的所有静态的XML定义。所有这一切都具有权重所有的地方,虽然它看起来正确,我想看到的事情居然有合适的高度和所有。问题是,不管在哪里我叫.getHeight()为我的格式布局我得到了0。我想在这两个的onCreate()和ONSTART()。一样。偏偏所有的UI对象了。你知道吗?
包com.app.conekta;
进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Window;
进口android.widget.Button;
进口android.widget.FrameLayout;
进口android.widget.Toast;
公共类Conekta延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
}
@覆盖
公共无效的OnStart(){
super.onStart();
}
@覆盖
公共无效onResume(){
super.onResume();
的FrameLayout FL1 =(的FrameLayout)findViewById(R.id.headerFrameLayout);
的FrameLayout FL2 =(的FrameLayout)findViewById(R.id.footerFrameLayout);
按钮B =(按钮)findViewById(R.id.searchButton);
Log.d(CONEKTA,将String.valueOf(b.getHeight()));
}
}
XML:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<的FrameLayout
机器人:ID =@ + ID / headerFrameLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.05
机器人:后台=#597eAA>
< ImageView的
机器人:ID =@ + ID / logoImage
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:后台=#7ba1d1
机器人:SRC =@可绘制/ logo_conekta/>
< /的FrameLayout>
<的LinearLayout
机器人:ID =@ + ID / bodyLinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.7
机器人:后台=#f3f3f3
机器人:方向=横向>
<的FrameLayout
机器人:ID =@ + ID / leftBlankFrameLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =0.15
机器人:后台=#f3f3f3>
< /的FrameLayout>
<的LinearLayout
机器人:ID =@ + ID / centerVerticalLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =0.7
机器人:方向=垂直>
<的FrameLayout
机器人:ID =@ + ID / topCenterFrameLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.35>
< /的FrameLayout>
<的TextView
机器人:ID =@ + ID / venueLabel
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.025
机器人:文本=你在看什么呢?
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字颜色=#000000/>
<的EditText
机器人:ID =@ + ID / venueTextField
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.025>
<不是requestFocus />
< /的EditText>
<的FrameLayout
机器人:ID =@ + ID / middleCenterFrameLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.05>
< /的FrameLayout>
<的TextView
机器人:ID =@ + ID / locationLabel
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.025
机器人:文本=在哪里?
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字颜色=#000000/>
< AutoCompleteTextView
机器人:ID =@ + ID / locationTextField
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.025
机器人:文本=/>
<的LinearLayout
机器人:ID =@ + ID / buttonLinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.05
机器人:后台=#f3f3f3
机器人:方向=横向>
<的FrameLayout
机器人:ID =@ + ID / leftButtonLinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =0.1>
< /的FrameLayout>
<按钮
机器人:ID =@ + ID / searchButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =0.8
机器人:后台=#6fa8dc
机器人:文本=搜索/>
<的FrameLayout
机器人:ID =@ + ID / rightButtonLinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =0.1>
< /的FrameLayout>
< / LinearLayout中>
<的FrameLayout
机器人:ID =@ + ID / bottomCenterFrameLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.35>
< /的FrameLayout>
< / LinearLayout中>
<的FrameLayout
机器人:ID =@ + ID / rightBlankFrameLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =0.15
机器人:后台=#f3f3f3>
< /的FrameLayout>
< / LinearLayout中>
<的FrameLayout
机器人:ID =@ + ID / footerFrameLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0.15
机器人:后台=#7ba1d1>
< /的FrameLayout>
< / LinearLayout中>
总之,意见不建但在的onCreate(),ONSTART(),或onResume()。因为它们在技术上不存在(至于的ViewGroup而言),它们的尺寸是0
在长,你可以去这里如何处理它一个更好的解释。
<一个href="http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r/4406090#4406090">How您检索视图的尺寸是多少? getHeight()都会和的getWidth()总是返回零
I'm building a UI, and it's all static defined in the XML. All of it has weights all over the place, and while it looks right, I wanted to see that things actually have the right height and all. The problem is that no matter where I call .getHeight() for my format layout I got 0. I tried in both onCreate() and onStart(). Same thing. Happens for all UI objects too. Any idea?
package com.app.conekta;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class Conekta extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
FrameLayout fl1 = (FrameLayout) findViewById(R.id.headerFrameLayout);
FrameLayout fl2 = (FrameLayout) findViewById(R.id.footerFrameLayout);
Button b=(Button) findViewById(R.id.searchButton);
Log.d("CONEKTA", String.valueOf(b.getHeight()));
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/headerFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#597eAA" >
<ImageView
android:id="@+id/logoImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#7ba1d1"
android:src="@drawable/logo_conekta" />
</FrameLayout>
<LinearLayout
android:id="@+id/bodyLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:background="#f3f3f3"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/leftBlankFrameLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.15"
android:background="#f3f3f3" >
</FrameLayout>
<LinearLayout
android:id="@+id/centerVerticalLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.7"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/topCenterFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.35" >
</FrameLayout>
<TextView
android:id="@+id/venueLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.025"
android:text="What are you looking for?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="@+id/venueTextField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.025" >
<requestFocus />
</EditText>
<FrameLayout
android:id="@+id/middleCenterFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05" >
</FrameLayout>
<TextView
android:id="@+id/locationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.025"
android:text="Where?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<AutoCompleteTextView
android:id="@+id/locationTextField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.025"
android:text="" />
<LinearLayout
android:id="@+id/buttonLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#f3f3f3"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/leftButtonLinearLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.1" >
</FrameLayout>
<Button
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.8"
android:background="#6fa8dc"
android:text="Search" />
<FrameLayout
android:id="@+id/rightButtonLinearLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.1" >
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/bottomCenterFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.35" >
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/rightBlankFrameLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.15"
android:background="#f3f3f3" >
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/footerFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:background="#7ba1d1" >
</FrameLayout>
</LinearLayout>
In short, the views are not built yet in onCreate(), onStart(), or onResume(). Since they technically don't exist (as far as the ViewGroup is concerned), their dimensions are 0.
In long, you can go here for a better explanation on how to handle it.
How do you to retrieve dimensions of a view? Getheight() and Getwidth() always return zero
这篇关于的getHeight返回0,所有的Android UI对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!