首先在res目錄下新建anim目錄,在anim目錄下建兩個動畫效果文件,用來控制菜單的彈出和隱藏:
image.png
popshow_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
上面的是菜單彈出時的效果, translate 位置轉移動畫效果 duration 屬性爲動畫持續時間 ,fromXDelta 屬性爲動畫起始時 X座標上的位置 toYDelta 屬性爲動畫結束時 Y座標上的位置 ,屬性裏面還可以加上%和p,例如:
android:toXDelta=”100%”,表示自身的100%,也就是從View自己的位置開始。
android:toXDelta=”80%p”,表示父層View的80%,是以它父層View爲參照的。
pophidden_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
然後在style.xml裏新建菜單的style:
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定顯示的動畫xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的動畫xml -->
</style>
然後是layout目錄下:新建名爲:pop.xml 的layout文件,佈局彈出的窗口:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="vertical"
android:background="#66999999"
>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_pop_name"
android:textSize="16sp"
android:textColor="#fff"
android:layout_width="wrap_content"
android:text="一生所愛"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_pop_title"
android:layout_marginLeft="20dp"
android:textSize="16sp"
android:textColor="#99ffffff"
android:layout_width="wrap_content"
android:text="西遊"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_pop_share"
android:gravity="right"
android:layout_marginLeft="20dp"
android:textSize="16sp"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:text="分享"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:layout_margin="6dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/back"/>
<TextView
android:id="@+id/tv_pop_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="花時間嗲及時地回家大家覅佛按史都華黃牛黨護身符"/>
<TextView
android:layout_margin="6dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/back"/>
<TextView
android:id="@+id/tv_pop_cancel"
android:textColor="#fff"
android:textSize="20sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"/>
</LinearLayout>
這個就不多說了。簡單的一個佈局。
在activity_main.xml文件裏新建一個Button:用來點擊:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="openPopWindow"
android:text="點擊可以彈出菜單" />
</LinearLayout>
同樣button被指定了一個一個響應的函數,即openPopWindow。
做好前面的之後就可以寫MainActivity了:
public class MainActivity extends AppCompatActivity {
private PopupWindow popupWindow;
private View contentView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showPopwindow();
}
/**
* 顯示popupWindow
*/
private void showPopwindow() {
//加載彈出框的佈局
contentView = LayoutInflater.from(MainActivity.this).inflate(
R.layout.pop, null);
popupWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);// 取得焦點
//注意 要是點擊外部空白處彈框消息 那麼必須給彈框設置一個背景色 不然是不起作用的
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//點擊外部消失
popupWindow.setOutsideTouchable(true);
//設置可以點擊
popupWindow.setTouchable(true);
//進入退出的動畫,指定剛纔定義的style
popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
// 按下android回退物理鍵 PopipWindow消失解決
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){
if(popupWindow!=null&&popupWindow.isShowing()){
popupWindow.dismiss();
return true;
}
}
return false;
}
/**
* 按鈕的監聽
* @param v
*/
public void openPopWindow(View v) {
//從底部顯示
popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
}
}
加入水平滑動控件
<HorizontalScrollView
android:id="@+id/hl_style"
android:layout_width="match_parent"
android:layout_height="@dimen/height25px"
android:layout_marginTop="@dimen/height14px"
android:layout_marginLeft="@dimen/width27px"
>
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
子空間佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/shuibiao1"
android:layout_marginLeft="@dimen/width7px"
android:layout_marginTop="@dimen/height13px"
/>
<TextView
android:id="@+id/tv_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#42B6FB"
android:layout_marginLeft="@dimen/width5px"
android:text=""
android:textSize="@dimen/width12px"
android:layout_marginTop="@dimen/height11px"
android:background="@drawable/month_selector"
/>
</LinearLayout>
private void showPopwindow() {
//加載彈出框的佈局
contentView = LayoutInflater.from(pipeListenerActivity.this).inflate(
R.layout.item_marker_info, null);
popupWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
name = popupWindow.getContentView().findViewById(R.id.tv_name);
lat = popupWindow.getContentView().findViewById(R.id.tv_lat);
adress = popupWindow.getContentView().findViewById(R.id.tv_adress);
phone = popupWindow.getContentView().findViewById(R.id.tv_phone);
manger = popupWindow.getContentView().findViewById(R.id.tv_manger);
time = popupWindow.getContentView().findViewById(R.id.tv_time);
rvMarker = popupWindow.getContentView().findViewById(R.id.rv_marke);
styleViewAdapter = popupWindow.getContentView().findViewById(R.id.hl_style);
styleinfoViewAdapter = popupWindow.getContentView().findViewById(R.id.hl_styleinfo);
mLinearLayout= popupWindow.getContentView().findViewById(R.id.linear);
mLinearLayout1= popupWindow.getContentView().findViewById(R.id.linear1);
styleViewAdapter = popupWindow.getContentView().findViewById(R.id.hl_style);
styleinfoViewAdapter = popupWindow.getContentView().findViewById(R.id.hl_styleinfo);
popupWindow.setFocusable(true);// 取得焦點
//注意 要是點擊外部空白處彈框消息 那麼必須給彈框設置一個背景色 不然是不起作用的
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//點擊外部消失
popupWindow.setOutsideTouchable(true);
//設置可以點擊
popupWindow.setTouchable(true);
//進入退出的動畫,指定剛纔定義的style
popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
// 按下android回退物理鍵 PopipWindow消失解決
}
for (int i = 0; i < styleList.size(); i++)
{
View view=LayoutInflater.from(pipeListenerActivity.this).inflate(R.layout.item_month1,mLinearLayout,false);
ImageView img= view.findViewById(R.id.iv_style);
TextView textView = view.findViewById(R.id.tv_month);
switch (styleList.get(i)){
case "a":
img.setImageResource(R.mipmap.li);
break;
case "b":
img.setImageResource(R.mipmap.ya);
break;
}
textView.setText(styleList.get(i));
mLinearLayout.addView(view);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}