想实现图中这样中间有半透明窗体的效果么?
Android 在列表List中显示半透明小窗体效果的控件,多的不多直接上代码,要说的都在注释里了:
Android 在列表List中显示半透明小窗体效果的控件,多的不多直接上代码,要说的都在注释里了:
import android.content.Context; import android.graphics.PixelFormat; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; import android.widget.TextView; /** * 窗口中间显示半透明提示框的小组件 * 用法:1. new 2. showPrevLetter(String prevLetter) * @author LL * */ public class PrevLetterDialog { //窗体消失的延时时间 private static final int DELAYED_HIDEN = 500; // 实现Runnable接口的类 private RemoveWindow mRemoveWindow = new RemoveWindow(); //在Handler里能实现对UI线程的修改 private Handler mHandler; //窗体管理器 private WindowManager mWindowManager; //显示在窗体中间半透明的字体框 private TextView mDialogText; //是否正在显示中 private boolean mShowing; //是否准备好显示了 private boolean mReady; //显示在窗体中的字符 private char mPrevLetter = Character.MIN_VALUE; /** * 注明:context一定要在可见UI中通过getContext()获得,否则会抛异常。 * @param context */ public PrevLetterDialog(Context context) { //得到窗体管理器 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); //得到布部创建者 LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //得到TextView mDialogText = (TextView) inflate.inflate(R.layout.list_position, null); //设TextView为可见 mDialogText.setVisibility(View.INVISIBLE); //通过handler把mDialogText展示出来 mHandler = new Handler(); //要用post可实现在多线程里调用UI线程并改变 mHandler.post(new Runnable() { public void run() { mReady = true; //窗体的布局参数 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); //把TextView添加到窗体中去 mWindowManager.addView(mDialogText, lp); } }); } //设置可见性 public void setmReady(boolean mReady) { this.mReady = mReady; } /** * 一般用在onScroll方法中,参数是字符串,不能为空,会取第一个字符显示 * @param prevLetter */ public void showPrevLetter(String prevLetter) { if (!mReady) { return; } char firstLetter = prevLetter.charAt(0); if (!mShowing && firstLetter != mPrevLetter) { mShowing = true; mDialogText.setVisibility(View.VISIBLE); } //设TextView显示的字体 mDialogText.setText(((Character) firstLetter).toString()); //移除mRemoveWindow实现的Runnable接口类 mHandler.removeCallbacks(mRemoveWindow); //添加mRemoveWindow实现的Runnable接口类,并设延时 mHandler.postDelayed(mRemoveWindow, DELAYED_HIDEN); mPrevLetter = firstLetter; } /** * 实现Runnable接口的类 */ private final class RemoveWindow implements Runnable { public void run() { removeWindow(); } } private void removeWindow() { if (mShowing) { mShowing = false; mDialogText.setVisibility(View.INVISIBLE); } } }
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:textSize="50sp" android:textColor="@color/band_common_content" android:background="@drawable/common_list_title" android:minWidth="70dip" android:layout_height="wrap_content" android:padding="5dip" android:gravity="center" />