PopupWindow组件实现浮动窗口

1、PopupWindow组件实现的是浮动窗口的功能,该组件需要通过new对象的形式来动态创建。

但创建后也需要设置窗口里面的内容的样式,因此也需要声明其布局文件。

在layout下建立一个文件。

<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:background="#000000"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/show_version"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="查看版本"
        android:textColor="#ffffff"
        android:textSize="14sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ffffff" />

    <TextView
        android:id="@+id/about_auth"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="关于作者"
        android:textColor="#ffffff"
        android:textSize="14sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ffffff" />

    <TextView
        android:id="@+id/exit"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="退出"
        android:textColor="#ffffff"
        android:textSize="14sp" />

</LinearLayout>

显示浮动窗口时,需要设置其大小。

settingBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (win == null) {
					win = new PopupWindow(Globals.SCREEN_WIDTH / 4,
							Globals.SCREEN_HEIGHT / 6);
					View v = LayoutInflater.from(BaseActivity.this).inflate(
							R.layout.window_layout, null);
					// 设置其布局
					win.setContentView(v);

					// 加入这些内部按钮的监听
					// 分别取得这些组件
					TextView versionText = (TextView) v
							.findViewById(R.id.show_version);
					TextView authText = (TextView) v
							.findViewById(R.id.about_auth);
					TextView exitText = (TextView) v.findViewById(R.id.exit);

					versionText.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							Toast.makeText(BaseActivity.this, "当前版本是: 1.0",
									Toast.LENGTH_LONG).show();
							win.dismiss();
						}
					});
					authText.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							Builder builder = new Builder(BaseActivity.this);
							builder.setTitle("提示");
							builder.setMessage("本作品由北航发布, 欢迎使用!");
							builder.setPositiveButton("关闭",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
										}
									});
							builder.create().show();
							win.dismiss();
						}
					});
					exitText.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							// 退出程序
						}
					});

				}
				if (win.isShowing()) {
					// 隐藏
					win.dismiss();
				} else {
					// 显示
					win.showAsDropDown(settingBtn);
				}
			}
		});

2、评分组件

通过RatingBar评分组件,可以实现打分功能的显示。

<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" >

    <RatingBar
        android:id="@+id/rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="3"
        android:stepSize="0.5" />

</LinearLayout>

public class MainActivity extends Activity {

	private RatingBar rating;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 设置默认使用的布局文件
		setContentView(R.layout.activity_main);

		rating = (RatingBar) findViewById(R.id.rating);

		rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				Toast.makeText(MainActivity.this, "得分: " + rating,
						Toast.LENGTH_SHORT).show();
			}
		});

	}
}



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值