gpt4 book ai didi

android - 从 Visibility GONE 设置 Visibility Visible 后未获取 RelativeLayout get Height()

转载 作者:行者123 更新时间:2023-11-29 18:03:26 24 4
gpt4 key购买 nike

我想要的是,当我点击 Dashboard Button 时,它会像 SlidingDrawer 一样打开,打开后再次点击它会关闭。我使用这个自定义抽屉是因为 SlidingDrawer 已弃用。

现在的问题是,它正常工作,除了第一次单击按钮时,它会非常快速地打开,没有任何动画,但正常关闭,之后工作正常。

我发现问题是当我使 RelativeLayout 可见并尝试计算它的 getHeight() 时,它最初会给出零,然后给出适当的高度。

这是 LogCat:

enter image description here这是我的 XML 文件。

 <?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>

这是代码::

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() 方法。

@
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

最佳答案

首次创建 View 时,在调用 onMeasure() 之前,您无法使用它的宽度和高度。因此,您第一次尝试获取值时,它们尚未分配。

由于您使用的是自定义 View ,因此解决方案非常简单。只需将获取高度和宽度的代码移动到 onMeasure() 并在之后使用它。

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

width = getWidth(); // Get View Width
height = getHeight();// Get View Height
}

关于android - 从 Visibility GONE 设置 Visibility Visible 后未获取 RelativeLayout get Height(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14597890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com