1.首先写入布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="啦啦"/>
</LinearLayout>
<RelativeLayout
android:layout_alignParentRight="true"
android:id="@+id/search_linear"
android:layout_width="35dp"
android:layout_height="24dp"
android:background="@drawable/cinema_search_shape">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/search_image"
android:layout_width="24dp"
android:layout_height="24dp"
android:paddingLeft="6dp"
android:src="@mipmap/com_icon_search_default" />
<EditText
android:id="@+id/search_edname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:background="@null"
android:ems="8"
android:padding="5dp"
android:textColor="#f2f2f2"
android:textSize="12sp"/>
<TextView
android:id="@+id/search_textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingTop="3dp"
android:text="搜索"
android:textColor="#ffffff"
android:textSize="12sp"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
2.背景设置
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="#f66b1c"
android:startColor="#fad961" />
<corners
android:bottomLeftRadius="12dp"
android:topLeftRadius="12dp" />
</shape>
3.主页面代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView search_image;
private EditText search_edname;
private TextView search_textName;
private RelativeLayout search_linear;
private AutoTransition transition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
search_image = (ImageView) findViewById(R.id.search_image);
search_edname = (EditText) findViewById(R.id.search_edname);
search_textName = (TextView) findViewById(R.id.search_textName);
search_linear = (RelativeLayout) findViewById(R.id.search_linear);
//点击事件
search_image.setOnClickListener(this);
search_textName.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.search_image:
//点击搜索 伸展
initExpand();
break;
case R.id.search_textName:
//点击text收缩
initReduce();
break;
}
}
/*设置伸展状态时的布局*/
public void initExpand() {
search_edname.setHint("CGV影城");
search_edname.requestFocus();
search_edname.setHintTextColor(Color.WHITE);
search_textName.setText("搜索");
search_textName.setVisibility(View.VISIBLE);
search_edname.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams LayoutParams = (LinearLayout.LayoutParams) search_linear.getLayoutParams();
LayoutParams.width = dip2px(170);
LayoutParams.setMargins(dip2px(0), dip2px(0), dip2px(0), dip2px(0));
search_linear.setLayoutParams(LayoutParams);
search_edname.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
search_edname.setFocusable(true);
search_edname.setFocusableInTouchMode(true);
return false;
}
});
//开始动画
beginDelayedTransition(search_linear);
}
/*设置收缩状态时的布局*/
private void initReduce() {
search_edname.setCursorVisible(false);
search_edname.setVisibility(View.GONE);
search_textName.setVisibility(View.GONE);
LinearLayout.LayoutParams LayoutParams = (LinearLayout.LayoutParams) search_linear.getLayoutParams();
LayoutParams.width = dip2px(40);
LayoutParams.setMargins(dip2px(0), dip2px(0), dip2px(0), dip2px(0));
search_linear.setLayoutParams(LayoutParams);
//隐藏键盘
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getWindowToken(), 0);
search_edname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
search_edname.setCursorVisible(true);
}
});
//开始动画
beginDelayedTransition(search_linear);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void beginDelayedTransition(ViewGroup view) {
transition = new AutoTransition();
transition.setDuration(500);
TransitionManager.beginDelayedTransition(view, transition);
}
private int dip2px(float dpVale) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dpVale * scale + 0.5f);
}
}