android使用popupwindow仿微信点赞功能
生活随笔
收集整理的這篇文章主要介紹了
android使用popupwindow仿微信点赞功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現微信朋友圈的點贊功能,代碼很簡單,就是設置一個布局,然后使用popupwindow讓這個布局顯示出來就可以了
實現效果圖:
1.主界面的布局就是一個listview,這里就不貼了,需要的話請自己補上
2.使用baseadapter設置listview條目顯示的內容,布局如下listview_item.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:orientation="vertical" android:layout_width="match_parent" android:layout_height="50dp"><Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="點贊" /><TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="74dp" android:layout_marginStart="74dp" android:text="用戶名" /><ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignEnd="@+id/textView3" android:layout_alignParentTop="true" android:layout_alignRight="@+id/textView3" android:layout_marginEnd="43dp" android:layout_marginRight="43dp" app:srcCompat="@mipmap/a1" /></RelativeLayout>3.接下來新建一個布局文件,作為點贊的彈框的布局,布局如下layout_more.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayout android:layout_width="200dp" android:layout_height="40dp" android:orientation="horizontal" android:gravity="center" android:background="@android:color/black"><ImageView android:layout_width="20dp" android:layout_height="20dp" android:src="@mipmap/ic_launcher"/><TextView android:id="@+id/like" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="贊" android:layout_marginLeft="20dp" android:textColor="@android:color/white"/><ImageView android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="20dp" android:src="@mipmap/ic_launcher"/><TextView android:id="@+id/comment" android:textColor="@android:color/white" android:layout_width="wrap_content" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:text="評論" /></LinearLayout> </LinearLayout>
5.在MainActivity中調用popupwindow,并且設置適配器顯示內容
public class MainActivity extends AppCompatActivity {//參考網址:http://www.cnblogs.com/ganchuanpu/p/6753098.html private PopupWindow mMorePopupWindow;private int mShowMorePopupWindowWidth;private int mShowMorePopupWindowHeight;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView lv = (ListView) findViewById(R.id.listview);lv.setAdapter(new MyAdapter(MainActivity.this));}class MyAdapter extends BaseAdapter {private Context context;public MyAdapter(Context context ) {this.context = context;}@Override public int getCount() {return 10;}@Override public Object getItem(int i) {return null;}@Override public long getItemId(int arg0) {return arg0;}@Override public View getView(int position, View convertView, ViewGroup parent) {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);convertView = inflater.inflate(R.layout.listview_item, null, false);Button moreBtn = (Button)convertView.findViewById(R.id.button);// 更多按鈕的點擊事件 moreBtn.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {showMore(v);}});return convertView;}/** * 彈出點贊和評論框 * * @param moreBtnView */ private void showMore(View moreBtnView) {if (mMorePopupWindow == null) {LayoutInflater li = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View content = li.inflate(R.layout.layout_more, null, false);mMorePopupWindow = new PopupWindow(content, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);mMorePopupWindow.setBackgroundDrawable(new BitmapDrawable());mMorePopupWindow.setOutsideTouchable(true);mMorePopupWindow.setTouchable(true);content.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);mShowMorePopupWindowWidth = content.getMeasuredWidth();mShowMorePopupWindowHeight = content.getMeasuredHeight();View parent = mMorePopupWindow.getContentView();TextView like = (TextView) parent.findViewById(R.id.like);TextView comment = (TextView) parent.findViewById(R.id.comment);// 點贊的監聽器 like.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);alert.setTitle("點贊");alert.setNegativeButton("取消", null);alert.show();}});// 評論的監聽器 comment.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);alert.setTitle("評論");alert.setNegativeButton("取消", null);alert.show();}});}if (mMorePopupWindow.isShowing()) {mMorePopupWindow.dismiss();} else {int heightMoreBtnView = moreBtnView.getHeight();mMorePopupWindow.showAsDropDown(moreBtnView, -mShowMorePopupWindowWidth,-(mShowMorePopupWindowHeight + heightMoreBtnView) / 2);}}} }然后運行即可,就可以實現微信點贊的效果啦總結
以上是生活随笔為你收集整理的android使用popupwindow仿微信点赞功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue的created的使用示例
- 下一篇: Python学习 Day38 jQue