我有一个正常的相对布局,带有文本视图和进度条。
现在,由于我有一个固定的布局宽度和高度,文本正确放置在中心并且看起来很好,在布局上我们正在将进度条的可见性更改为" Visible",但是因为我有一个固定的宽度,进度条位于文本的顶部。
我想要实现的是,onclick增加布局的右端宽度和动画。
这是我的代码:
<RelativeLayout
android:id="@+id/rellyt"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_margin="5dp"
android:background="#B7E4FF"
android:clickable="true" >
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:text="click on this button"
android:textColor="#000000"
android:textSize="14sp" />
<ProgressBar
android:id="@+id/prgbar"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:visibility="visible" />
</RelativeLayout>
布局的屏幕截图:
答案 0 :(得分:1)
动画
public class ResizeWidthAnimation extends Animation
{
private int mWidth;
private int mStartWidth;
private View mView;
public ResizeWidthAnimation(View view, int width)
{
mView = view;
mWidth = width;
mStartWidth = view.getWidth();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime);
mView.getLayoutParams().width = newWidth;
mView.requestLayout();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
public boolean willChangeBounds()
{
return true;
}
}
<强>用法强>
if(animate)
{
ResizeWidthAnimation anim = new ResizeWidthAnimation(leftFrame, leftFragmentWidthPx);
anim.setDuration(500);
leftFrame.startAnimation(anim);
}
else
{
this.leftFragmentWidthPx = leftFragmentWidthPx;
LayoutParams lp = (LayoutParams) leftFrame.getLayoutParams();
lp.width = leftFragmentWidthPx;
leftFrame.setLayoutParams(lp);
}
答案 1 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
<强> main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="@+id/customLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#B7E4FF"
android:gravity="center"
android:padding="5dp">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click on this button"
android:textColor="#000000"
android:textSize="14sp" />
<ProgressBar
android:id="@+id/prgbar"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
<强> MainActivity.java 强>
public class MainActivity extends Activity {
private LinearLayout customLayout;
private ProgressBar prgbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
customLayout = (LinearLayout) findViewById(R.id.customLayout);
prgbar = (ProgressBar) findViewById(R.id.prgbar);
customLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(prgbar.getVisibility() == View.INVISIBLE){
prgbar.setVisibility(View.VISIBLE);
}else{
prgbar.setVisibility(View.INVISIBLE);
}
}
});
}
}