从GONE的可见设置能见度可见后没有得到的RelativeLayout的的getHeight() [英] Not getting RelativeLayout's getHeight() after setting Visibility Visible from Visiblity GONE

查看:264
本文介绍了从GONE的可见设置能见度可见后没有得到的RelativeLayout的的getHeight()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的是,当我在仪表板按钮点击它会打开像SlidingDrawer和后点击它时,它再次打开它将会关闭。
我用这个自定义的抽屉,因为SlidingDrawer是德precated。

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.

我发现的问题是,当我做的RelativeLayout可见,并尝试计算其的getHeight(),它会给予零开始,之后给出了适当的高度。

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.

这里的LogCat中:

here's LogCat:


这里是我的XML文件。

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>

这里的code:

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
        }
    };
}

使用onMeasure()方法。

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() );

    }

推荐答案

下面就是我得到了这个问题的解决方案。

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.

所以我所做的是我用以下code得到的屏幕运行时的实际高度和宽度。

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; 

和之后,我得到了屏幕的高度和宽度,充气视图。
之后得到了linearlayouts高度和使用以下code新的高度添加到它。

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);

和它的工作!

这篇关于从GONE的可见设置能见度可见后没有得到的RelativeLayout的的getHeight()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆