Not getting RelativeLayout's getHeight() after

2020-03-30 16:32发布

What i want is, when i click on Dashboard Button it will open like a SlidingDrawer and after it opened when clicked on it again it will close. i use this custom drawer because SlidingDrawer is Deprecated.

now the problem is, its working properly except first time when i click on Button it'll open really fast without any animation, but closes properly and after that works fine.

i found the problem is when i make RelativeLayout Visible and try to calculate its getHeight() it will give zero initially, and after that gives proper height.

here's LogCat:

enter image description here Here's my XML File.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relLayOne"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#0000" >

    <Button
        android:id="@+id/btnNEWCLICK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/dashboard" />

    <RelativeLayout
        android:id="@+id/relLayTwo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnNEWCLICK"
        android:background="#000"
        android:visibility="gone" >

        <Button
            android:id="@+id/loc"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="#000"
            android:gravity="left|center_vertical"
            android:paddingLeft="5dp"
            android:text="My Location"
            android:textColor="#fff"
            android:textSize="20sp" />

        <View
            android:layout_width="0.1dp"
            android:layout_height="108dp"
            android:layout_centerInParent="true"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:background="#fff" />

        <Button
            android:id="@+id/phot"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="#000"
            android:gravity="left|center_vertical"
            android:paddingLeft="5dp"
            android:text="Photos"
            android:textColor="#fff"
            android:textSize="20sp" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.1dp"
            android:layout_below="@+id/phot"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#fff" />

        <Button
            android:id="@+id/free"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/loc"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="#000"
            android:gravity="left|center_vertical"
            android:paddingLeft="5dp"
            android:text="Free stuff"
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:id="@+id/leade"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/phot"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="#000"
            android:gravity="left|center_vertical"
            android:paddingLeft="5dp"
            android:text="Leaderboard"
            android:textColor="#fff"
            android:textSize="20sp" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.1dp"
            android:layout_below="@+id/leade"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#fff" />

        <Button
            android:id="@+id/live"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/free"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="#000"
            android:gravity="left|center_vertical"
            android:paddingLeft="5dp"
            android:text="Live Action"
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:id="@+id/home"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/leade"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="#000"
            android:gravity="left|center_vertical"
            android:paddingLeft="5dp"
            android:text="Home"
            android:textColor="#fff"
            android:textSize="20sp" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.1dp"
            android:layout_below="@+id/home"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#fff" />

        <Button
            android:id="@+id/app"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/live"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="#000"
            android:gravity="left|center_vertical"
            android:paddingLeft="5dp"
            android:text="App stats"
            android:textColor="#fff"
            android:textSize="20sp" />
    </RelativeLayout>

</RelativeLayout>

Here's The Code::

public class MoveView extends LinearLayout{
     private Button openCloseButton;
     private boolean isVisible = false;
     private RelativeLayout relLayTwo, relLayOne;
     private float animationHeight = 300.0f;

    public MoveView(Context context , AttributeSet attr) {
        super(context, attr);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.in, this);

        relLayTwo = (RelativeLayout) findViewById(R.id.relLayTwo);
        //relLayOne = (RelativeLayout) findViewById(R.id.relLayOne);
        openCloseButton = (Button) findViewById(R.id.btnNEWCLICK);

        openCloseButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                toggle();
            }
        });

        Log.d("test", "before "+relLayTwo.getHeight());
        if (relLayTwo.getHeight() == 0){
            relLayTwo.setVisibility(View.VISIBLE);
            relLayTwo.invalidate();
            relLayTwo.refreshDrawableState();
            //rel.setVisibility(View.INVISIBLE);
            relLayTwo.setVisibility(View.GONE);
            Log.d("test", "after "+relLayTwo.getHeight());
        }

    }



    public void toggle() {
        TranslateAnimation anim = null;
        isVisible = !isVisible;

        if (isVisible) {
            relLayTwo.setVisibility(View.VISIBLE);
            anim = new TranslateAnimation(0.0f, 0.0f, relLayTwo.getHeight(), 0.0f);
            Log.d("test", " rel Height "+relLayTwo.getHeight());
        } else {
            Log.d("test", " rel Height else "+relLayTwo.getHeight() );
            anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, relLayTwo.getHeight());
            anim.setAnimationListener(collapseListener);
        }

        anim.setDuration(600);
        anim.setInterpolator(new AccelerateInterpolator(1.0f));
        startAnimation(anim);
    }

    Animation.AnimationListener collapseListener = new Animation.AnimationListener() {
        public void onAnimationEnd(Animation animation) {
            relLayTwo.setVisibility(View.GONE);
        }

        public void onAnimationRepeat(Animation animation) {
            // not needed
        }

        public void onAnimationStart(Animation animation) {
            // not needed
        }
    };
}

by using onMeasure() method.

@
Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d("test", " RelativeLAyout In onMeaseure "+relLayTwo.getHeight() );

    }

enter image description here

enter image description here

enter image description here

4条回答
Melony?
2楼-- · 2020-03-30 16:36

Here's how i got solution for this problem.

the actual problem with getting layout's height when we set it to wrap content is, it will not give height until it will actually draw on a screen. and in my case the custom layout got called first and then it will call remaining layout, so there is not a chance for layout to get draw.

so what i did is i got actual height and width of the screen run time by using following code.

DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
        height  =   metrics.heightPixels;
        width   =   metrics.widthPixels; 

and after i got screens height and width, inflate view. after that got linearlayouts height and add new height to it using following code.

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relLayTwo.getLayoutParams();
    Log.d("test", "height "+params.height);
    params.height = height*40/100;
    relLayTwo.setLayoutParams(params);
    relLayTwo.setVisibility(View.GONE);

and its worked!!

查看更多
够拽才男人
3楼-- · 2020-03-30 16:38

When a View is first created, it's width and height are not available to you until onMeasure() has been called. Due to this, the first time you try to get the values, they have not been assigned yet.

Since you're using a custom View, the solution is pretty simple. Simply move the code that gets the height and width into onMeasure() and use it after that.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    width = getWidth(); // Get View Width
    height = getHeight();// Get View Height
} 
查看更多
仙女界的扛把子
4楼-- · 2020-03-30 16:44
<RelativeLayout
        android:id="@+id/relLayTwo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnNEWCLICK"
        android:background="#000"
        android:visibility="gone" > //don't put gone in xml

In OnViewCreated look for the height and after that make this view GONE

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
         relLayTwo = (RelativeLayout) findViewById(R.id.relLayTwo);
         mHeight = relLayTwo.getHeight();
         relLayTwo.setVisibility(View.GONE);}

In this way you will get the height because on onViewCreated the view is already drawn, and after you have what you need, make the view GONE.

查看更多
混吃等死
5楼-- · 2020-03-30 16:54

simply use this to get the height and width of which visibility is GONE..

myView.getMeasuredHeight();
myView.getMeasuredWidth();
查看更多
登录 后发表回答