获取LinearLayout Height的错误值?价值加倍。 Android 4.2.2

时间:2014-03-31 20:32:39

标签: android height android-linearlayout

我尝试在Activity上获得LinearLayout Height,但我得到的高度值加倍。 PS:身高是60但我得到120.

有人可以帮忙吗? 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pull_to_refresh_head1"
    android:layout_width="200dip"
    android:layout_height="60dip"
    android:layout_centerInParent="true"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world" />

</LinearLayout>

Java代码:

package com.example;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.ViewTreeObserver;
    import android.view.ViewTreeObserver.OnGlobalLayoutListener;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    public class GdomRefreshableActivity extends Activity {
        private LinearLayout l1;
        TextView t1;
        @Override
        //create
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_gdom_refreshable);

            l1=(LinearLayout)findViewById(R.id.pull_to_refresh_head1);
            t1=(TextView)findViewById(R.id.t1);          
                 ViewTreeObserver vto2 = l1.getViewTreeObserver();  
                    vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                        @Override  
                        public void onGlobalLayout() {
                            l1.getViewTreeObserver().removeGlobalOnLayoutListener(this);  
                            int h = l1.getHeight();
                             int ht = t1.getHeight();
                            Log.d("GdomRefreshableActivity2",String.valueOf(h));
                        }  
                    });
        }   

    }

0 个答案:

没有答案