我希望自己的应用能够倾听'点击'事件然后隐藏头部并扩展身体。
但似乎我只能将身体扩展到' MATCH_PARENT'最多。 当我将内容扩展到小于' MATCH_PARENT'的高度时,代码可以正常工作,但如果高度大于' MATCH_PARENT',那么它就无法正常工作。
这是我的布局XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#000">
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#e67e22"></RelativeLayout>
<RelativeLayout
android:id="@+id/content"
android:layout_below="@id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e74c3c"
></RelativeLayout>
</RelativeLayout>
这是我的活动:
公共类MainActivity扩展了ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout header = (RelativeLayout) findViewById(R.id.header);
final RelativeLayout content = (RelativeLayout) findViewById(R.id.content);
content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
header.animate().translationY(-header.getMeasuredHeight());
content.animate().translationY(-header.getMeasuredHeight());
// content.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
final int originalHeight = content.getMeasuredHeight();
final int targetHeight = content.getMeasuredHeight() + header.getMeasuredHeight();
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
content.getLayoutParams().height = interpolatedTime == 1
? targetHeight
: (int)(header.getMeasuredHeight()*interpolatedTime + originalHeight);
content.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(500);
content.startAnimation(a);
}
});
}
点击之前:
点击后
:
答案 0 :(得分:1)
是的,用LinearLayout替换外部RelativeLayout解决了我的问题。