我在EditText
内有TextView
和RelativeLayout
。EditText
的宽度为match-parent
,TextView
与结尾对齐EditText
。现在我想要的是当用户点击EditText
时,它的宽度应该在动画时减小。代码工作正常但问题是在减小宽度后我无法请参阅TextView
,即使它与EditText
XML
<RelativeLayout
android:id="@+id/ll_search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/tb_explore"
android:layout_marginLeft="05dp"
android:layout_marginRight="05dp"
android:layout_marginTop="07dp"
android:clipToPadding="false"
android:clipChildren="false"
android:focusable="false">
<RelativeLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/search_view">
<AutoCompleteTextView
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/imageView11"
android:layout_toStartOf="@+id/imageView11"
android:background="@android:color/transparent"
android:hint="Search by vendor name or keyword"
android:imeOptions="actionGo"
android:padding="10dp"
android:singleLine="true"
android:textSize="@dimen/regular" />
</RelativeLayout>
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/test"
android:gravity="center"
android:text="Cancel"
android:visibility="visible" />
</RelativeLayout>
动画制作时减小宽度的代码
ValueAnimator anim = ValueAnimator.ofInt(llSearch.getMeasuredWidth(), llSearch.getMeasuredWidth() - 200);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
if (Constants.isLoggingEnable) {
Logger.logError("Search lenght--", "" + llSearch.getMeasuredWidth());
Logger.logError("Lenght", "" + 500);
Logger.logError("Value of animation--", "" + (Integer) valueAnimator.getAnimatedValue());
}
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = llSearch.getLayoutParams();
layoutParams.width = val;
llSearch.setLayoutParams(layoutParams);
}
});
anim.setDuration(500);
anim.start();
答案 0 :(得分:1)
你去 -
ValueAnimator anim = ValueAnimator.ofInt(testLayout.getMeasuredWidth(), testLayout.getMeasuredWidth() - 200);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = testLayout.getLayoutParams();
layoutParams.width = val;
testLayout.setLayoutParams(layoutParams);
}
});
anim.setDuration(500);
anim.start();
}
其中testLayout为testLayout = (RelativeLayout) findViewById(R.id.test);
。您应该减小测试布局的宽度。 xml也是(非常微小的变化) -
<RelativeLayout
android:id="@+id/ll_search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="05dp"
android:layout_marginRight="05dp"
android:layout_marginTop="07dp"
android:clipChildren="false"
android:clipToPadding="false"
android:focusable="false">
<RelativeLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/colorPrimary">
<AutoCompleteTextView
android:id="@+id/et_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:hint="Search by vendor name or keyword"
android:imeOptions="actionGo"
android:padding="10dp"
android:singleLine="true"
android:textSize="12sp"/>
</RelativeLayout>
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/test"
android:layout_toRightOf="@+id/test"
android:gravity="center"
android:text="Cancel"
android:visibility="visible"/>
</RelativeLayout>