在做微信、微博、qq等分享时,一般是点击分享按钮后会从底部弹出滑动窗口,然后选择要分享的社交平台进行分享。今日头条、腾讯新闻等内容App的评论也是从底部滑动弹出输入窗口,进行评论输入的。本篇文章就讲讲怎么通过Activity实现底部弹出滑动窗口的。实现效果是通过Animation功能实现的,效果如下: 源码下载地址
主要代码如下:
一、滑动窗口
PopupShareActivity类
继承自Activity并实现了
OnClickListener,方便处理Click事件。代码如下:
public class
PopupShareActivity
extends
Activity
implements
View.OnClickListener {
@Override
protected void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_popup_share
);
//全屏Activity操作
getWindow().setLayout(WindowManager.LayoutParams.
FILL_PARENT
, WindowManager.LayoutParams.
FILL_PARENT
);
//QQ分享按钮
ImageBtn MyIBtn1 = (ImageBtn)findViewById(R.id.
MyIBtn_1
);
MyIBtn1.setImageResource(R.drawable.
ic_share_qq
);
MyIBtn1.setText(
"QQ"
);
ImageBtn MyIBtn2 = (ImageBtn)findViewById(R.id.
MyIBtn_2
);
MyIBtn2.setImageResource(R.drawable.
ic_share_sina
);
MyIBtn2.setText(
"微博"
);
ImageBtn MyIBtn3 = (ImageBtn)findViewById(R.id.
MyIBtn_3
);
MyIBtn3.setImageResource(R.drawable.
ic_share_wechat
);
MyIBtn3.setText(
"微信"
);
ImageBtn MyIBtn4 = (ImageBtn)findViewById(R.id.
MyIBtn_4
);
MyIBtn4.setImageResource(R.drawable.
ic_share_wxcircle
);
MyIBtn4.setText(
"朋友圈"
);
Button btn_cancel = (Button)
this
.findViewById(R.id.
btn_cancel
);
btn_cancel.setOnClickListener(
this
);
}
@Override
public void
onClick(View view) {
switch
(view.getId()) {
case
R.id.
btn_cancel
:
break
;
default
:
break
;
}
finish();
}
}
二、分享类布局
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
android
:layout_width=
"fill_parent"
android
:layout_height=
"wrap_content"
android
:gravity=
"center_horizontal"
android
:orientation=
"vertical"
>
<
LinearLayout
android
:id=
"@+id/pop_layout"
android
:layout_width=
"fill_parent"
android
:layout_height=
"wrap_content"
android
:gravity=
"center_horizontal"
android
:orientation=
"vertical"
android
:layout_alignParentBottom=
"true"
android
:background=
"@color/color_white"
>
<
LinearLayout
android
:id=
"@+id/share_layout"
android
:layout_width=
"fill_parent"
android
:layout_height=
"wrap_content"
android
:orientation=
"horizontal"
android
:layout_alignParentBottom=
"true"
android
:background=
"@color/color_white"
>
<
com.jxkj.bbccode.anxiuyunslid.component.ImageBtn
android
:id=
"@+id/MyIBtn_1"
android
:layout_height=
"wrap_content"
android
:layout_width=
"0dp"
android
:layout_weight=
"1.0"
android
:clickable=
"true"
android
:focusable=
"true"
/>
<
com.jxkj.bbccode.anxiuyunslid.component.ImageBtn
android
:id=
"@+id/MyIBtn_2"
android
:layout_width=
"0dp"
android
:layout_weight=
"1.0"
android
:layout_height=
"wrap_content"
android
:clickable=
"true"
android
:focusable=
"true"
/>
<
com.jxkj.bbccode.anxiuyunslid.component.ImageBtn
android
:id=
"@+id/MyIBtn_3"
android
:layout_width=
"0dp"
android
:layout_weight=
"1.0"
android
:layout_height=
"wrap_content"
android
:clickable=
"true"
android
:focusable=
"true"
/>
<
com.jxkj.bbccode.anxiuyunslid.component.ImageBtn
android
:id=
"@+id/MyIBtn_4"
android
:layout_width=
"0dp"
android
:layout_weight=
"1.0"
android
:layout_height=
"wrap_content"
android
:clickable=
"true"
android
:focusable=
"true"
/>
</
LinearLayout
>
<
Button
android
:id=
"@+id/btn_cancel"
android
:layout_marginLeft=
"20dip"
android
:layout_marginRight=
"20dip"
android
:layout_marginTop=
"15dip"
android
:layout_marginBottom=
"15dip"
android
:layout_width=
"fill_parent"
android
:layout_height=
"wrap_content"
android
:text=
"取消"
android
:textColor=
"#ffffff"
android
:textStyle=
"bold"
/>
</
LinearLayout
>
</
RelativeLayout
>
三、AndroidManifest添加Activity配置
<
activity
android
:name=
".activity.PopupShareActivity"
android
:theme=
"@style/PopupShareActivity"
>
四、
PopupShareActivity主题
<
style
name=
"MyDialogStyleBottom"
parent=
"Theme.AppCompat.Light.NoActionBar"
>
<
item
name=
"android:windowAnimationStyle"
>@style/AnimBottom</
item
>
<
item
name=
"android:windowFrame"
>@null</
item
>
<!-- 边框 -->
<
item
name=
"android:windowIsFloating"
>true</
item
>
<!-- 是否浮现在activity之上 -->
<
item
name=
"android:windowIsTranslucent"
>true</
item
>
<!-- 半透明 -->
<
item
name=
"android:windowNoTitle"
>true</
item
>
<!-- 无标题 -->
<
item
name=
"android:windowBackground"
>@android:color/transparent</
item
>
<!-- 背景透明 -->
<
item
name=
"android:backgroundDimEnabled"
>true</
item
>
<!-- 模糊 -->
</
style
>
<
style
name=
"AnimBottom"
parent=
"@android:style/Animation"
>
<
item
name=
"android:windowEnterAnimation"
>@anim/push_bottom_in</
item
>
<
item
name=
"android:windowExitAnimation"
>@anim/push_bottom_out</
item
>
</
style
>
五、
Animation动画效果
在anim目下下定义xml文件分别为:
<!-- 上下滑入式 -->
<
translate
android
:duration=
"200"
android
:fromYDelta=
"100%p"
android
:toYDelta=
"0"
/>
</
set
>
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<!-- 上下滑出式 -->
<
translate
android
:duration=
"200"
android
:fromYDelta=
"0"
android
:toYDelta=
"50%p"
/>
</
set
>