我有两个布局,其中一个是面板,另一个是主布局。 我想将面板移到右侧,这部分完成了。 问题是主要布局不能拉伸以填满屏幕, 当面板转移到右侧时,有一个空白区域(右侧面板的大小)。 我试图更改主要布局参数(宽度),但我的布局非常复杂,动画也不够流畅。
这是代码 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:orientation="horizontal" >
<LinearLayout
android:id="@+id/panel"
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@android:color/holo_green_dark"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3" />
</LinearLayout>
<LinearLayout
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/panel"
android:background="@android:color/holo_purple"
android:orientation="vertical" >
<Button
android:id="@+id/start_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
</LinearLayout>
java代码
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_mainLayout = (LinearLayout) findViewById(R.id.main_layout);
_panelLayout = (LinearLayout) findViewById(R.id.panel);
_startAnimation = (Button) findViewById(R.id.start_animation);
_startAnimation.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
startAnimation();
}
});
}
private void startAnimation()
{
final RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) _mainLayout.getLayoutParams();
final int screenWidth = 1280;
int mainLayoutXPositionStart = (int) _panelLayout.getX();
int mainLayoutXpositionEnd = screenWidth;
if (_mainLayoutOpen)
{
mainLayoutXpositionEnd = 1280 - _panelLayout.getMeasuredWidth();
mainLayoutXPositionStart = screenWidth;
}
ObjectAnimator mainLayoutAnimation = ObjectAnimator.ofInt(_panelLayout, "mainAnim", mainLayoutXPositionStart, mainLayoutXpositionEnd);
mainLayoutAnimation.addUpdateListener(new AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
int value = (Integer) animation.getAnimatedValue();
// lp.width = value;
_panelLayout.setX(value);
_mainLayout.requestLayout();
_panelLayout.requestLayout();
}
});
mainLayoutAnimation.setDuration(500);
mainLayoutAnimation.start();
_mainLayoutOpen = !_mainLayoutOpen;
}
更新:添加屏幕截图
动画之前
动画之后