Android 搜索跳到一个新的界面,基本上都是淡入淡出,没有跳转动画的。
MainActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/file_item_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<TextView
android:id="@+id/search_input"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_gravity="center|left"
android:layout_marginLeft="5dip"
android:layout_marginRight="3dp"
android:background="#F3F4F7"
android:drawableLeft="@mipmap/search"
android:drawablePadding="5dp"
android:ellipsize="end"
android:gravity="center_vertical"
android:hint="搜索"
android:imeOptions="actionSearch"
android:paddingLeft="2dp"
android:maxLines="1"
android:textColorHint="@color/gray_btn_bg_color"
android:textSize="15sp" />
</RelativeLayout>
这里用TextView 展示成搜索框的样子,点击进入下一个界面,MainActivity:
package com.dhl.searchfade;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView search_input ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search_input = (TextView)findViewById(R.id.search_input);
search_input.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SearchActivity.class);
startActivity(intent);
//淡入
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//overridePendingTransition(0, 0);
}
});
}
}
在startActivity 后面加上 overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)这句。
淡入的效果就出来了。
SearchActivity XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/search_parent_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginLeft="5dip"
android:background="#F3F4F7"
>
<EditText
android:id="@+id/search_input"
android:background="#00000000"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginLeft="5dip"
android:layout_marginRight="3dp"
android:drawableLeft="@mipmap/search"
android:drawablePadding="5dp"
android:ellipsize="end"
android:hint="搜索"
android:imeOptions="actionSearch"
android:focusable="true"
android:paddingLeft="2dp"
android:maxLines="1"
android:textColorHint="@color/gray_btn_bg_color"
android:textColor="#222222"
android:textSize="15sp" />
<TextView
android:id="@+id/search_dele"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:drawableLeft="@mipmap/search_clear"
android:layout_marginBottom="1dp"
android:layout_marginRight="5dp"
android:visibility="gone"
android:gravity="center_vertical"
/>
</LinearLayout>
<TextView
android:id="@+id/cancel_search"
android:layout_width="50dp"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#222222"
android:text="取消"
/>
</LinearLayout>
</LinearLayout>
一个EditText 一个取消按钮
Code:
public class SearchActivity extends AppCompatActivity {
private TextView textView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
//隐藏actionBar
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
textView = (TextView)findViewById(R.id.cancel_search);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
closeActivityByFade();
}
});
}
/**
* 淡入淡出
*/
private void closeActivityByFade()
{
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
@Override
public void onBackPressed() {
super.onBackPressed();
closeActivityByFade();
}
}
点击取消和返回键都展现淡出效果。
为SearchActivity 设置Manifest 配置文件:
<activity android:name=".SearchActivity"
android:windowSoftInputMode="stateVisible|adjustResize"
></activity>
这样当EditText 获得焦点时,弹出软键盘。
效果如下图:
录制的有点烂。。。