最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義PopWindow實(shí)現(xiàn)QQ、微信彈出菜單效果

 更新時(shí)間:2018年04月25日 10:53:11   作者:愛吃魚的老虎  
這篇文章主要為大家詳細(xì)介紹了Android自定義PopWindow實(shí)現(xiàn)QQ、微信彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前段時(shí)間在個(gè)人開發(fā)的項(xiàng)目中需要用到彈出菜單,類似QQ右上角的彈出菜單,自己使用popwin的次數(shù)也不是很多,其中也遇到過(guò)一點(diǎn)問題,今天正好有時(shí)間就把一些經(jīng)驗(yàn)分享給大家。

先來(lái)看看最終實(shí)現(xiàn)過(guò)后的效果怎么樣,下面放上圖

自定義的彈出菜單是繼承的popwin,并不是view 因?yàn)闆]有必要重復(fù)造車輪,如果想要實(shí)現(xiàn)某種特殊的效果另說(shuō)。首先創(chuàng)建類MyPopWindow繼承Popwindow。

public class MyPopWindow extends PopupWindow implements View.OnClickListener {
 private Context context;
 private View view;
 private LinearLayout scan;
 private LinearLayout add;
 public MyPopWindow(Context context) {
 this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 }

 public MyPopWindow(Context context, int width, int height) {
 super(context);
 this.context = context;
 setWidth(width);
 setHeight(height);
 setFocusable(true);
 setOutsideTouchable(true);
 setTouchable(true);
 view = LayoutInflater.from(context).inflate(R.layout.layout_mypopwin,null);
 setContentView(view);
 scan = (LinearLayout) view.findViewById(R.id.scan);
 add = (LinearLayout) view.findViewById(R.id.add);
 }

 @Override
 public void onClick(View view) {
 switch (view.getId()){
  case R.id.scan:

  break;
  case R.id.add:

  break;
 }
 }
}

下面給出最開始的布局文件

<?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:id="@+id/scan"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="掃一掃"
  android:textSize="16sp"/>
 </LinearLayout>
 <View
 android:layout_width="wrap_content"
 android:layout_height="0.5dp"
 android:background="#cdcdcd"/>
 <LinearLayout
 android:id="@+id/add"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="加好友"
  android:textSize="16sp"/>
 </LinearLayout>
</LinearLayout>

在activity中調(diào)用自定義彈出菜單看看目前的效果

調(diào)用的代碼MyPopWindow win = new MyPopWindow (MainActivity.this, 200,150);指定了彈出菜單的寬高,如果不給就會(huì)默認(rèn)給出wrap_content,就會(huì)沾滿整個(gè)屏幕的寬度。這個(gè)樣子還是比較簡(jiǎn)陋,現(xiàn)在在布局文件中加上.9圖的背景圖,在來(lái)看看效果

<?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"
 android:background="@drawable/title_function_bg">
 <LinearLayout
 android:id="@+id/scan"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="掃一掃"
  android:textSize="16sp"/>
 </LinearLayout>
 <View
 android:layout_width="wrap_content"
 android:layout_height="0.5dp"
 android:background="#cdcdcd"/>
 <LinearLayout
 android:id="@+id/add"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="加好友"
  android:textSize="16sp"/>
 </LinearLayout>
</LinearLayout>

運(yùn)行后的效果

美觀了一點(diǎn),但是背景后面還有背景什么情況,那么問題來(lái)了,怎么解決這個(gè)問題?那就需要在popwin的構(gòu)造方法中加入setBackgroundDrawable(new BitmapDrawable()),難看的方形背景就會(huì)消失了。

接近目標(biāo)效果了,現(xiàn)在的問題是,每次增加一個(gè)菜單項(xiàng)都要手動(dòng)的定制寬高很煩人,想讓它自己適應(yīng)高度、寬度,所以那就得修改布局文件了,想想android能夠自由增加item的控件不少,首先想到的就是listview。修改布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/title_function_bg">
 <ListView
 android:id="@+id/title_list"
 android:layout_width="120dp"
 android:layout_height="fill_parent"
 android:cacheColorHint="#00000000"
 android:divider="@drawable/popu_line"
 android:padding="3dp"
 android:scrollingCache="false"
 android:listSelector="@drawable/title_list_selector"/>
</LinearLayout>

然后修改自定義的popwindow

public class CustomWin extends PopupWindow {
 private Context context;
 private View view;
 private ListView listView;
 private List<String> list;

 public CustomWin(Context context) {
 this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 }

 public CustomWin(Context context, int with, int height) {
 this.context = context;
 setWidth(with);
 setHeight(height);
 //設(shè)置可以獲得焦點(diǎn)
 setFocusable(true);
 //設(shè)置彈窗內(nèi)可點(diǎn)擊
 setTouchable(true);
 //設(shè)置彈窗外可點(diǎn)擊
 setOutsideTouchable(true);
 setBackgroundDrawable(new BitmapDrawable());
 view = LayoutInflater.from(context).inflate(R.layout.popwin_menu,null);
 setContentView(view);
 setAnimationStyle(R.style.popwin_anim_style);
 initData();
 }

 private void initData() {
 listView = (ListView) view.findViewById(R.id.title_list);
 list = new ArrayList<String>();
 list.add("添加好友");
 list.add("掃一掃");
 list.add("支付寶");
 list.add("視頻聊天");
 //設(shè)置列表的適配器
 listView.setAdapter(new BaseAdapter() {
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 TextView textView = null;

 if (convertView == null) {
  textView = new TextView(context);
  textView.setTextColor(Color.rgb(255,255,255));
  textView.setTextSize(14);
  //設(shè)置文本居中
  textView.setGravity(Gravity.CENTER);
  //設(shè)置文本域的范圍
  textView.setPadding(0, 13, 0, 13);
  //設(shè)置文本在一行內(nèi)顯示(不換行)
  textView.setSingleLine(true);
 } else {
  textView = (TextView) convertView;
 }
 //設(shè)置文本文字
 textView.setText(list.get(position));
 //設(shè)置文字與圖標(biāo)的間隔
 textView.setCompoundDrawablePadding(0);
 //設(shè)置在文字的左邊放一個(gè)圖標(biāo)
 textView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap., null, null, null);

 return textView;
 }

 @Override
 public long getItemId(int position) {
 return position;
 }

 @Override
 public Object getItem(int position) {
 return list.get(position);
 }

 @Override
 public int getCount() {
 return list.size();
 }
 });
 }
}

最終效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解VirtualApk啟動(dòng)插件Activity

    詳解VirtualApk啟動(dòng)插件Activity

    這篇文章主要介紹了詳解VirtualApk啟動(dòng)插件Activity,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • 最新評(píng)論

    富阳市| 宁蒗| 富民县| 五台县| 丹凤县| 家居| 武宣县| 界首市| 山东| 永新县| 阜新市| 尉犁县| 加查县| 故城县| 万盛区| 河曲县| 寿宁县| 连南| 洱源县| 永定县| 宁强县| 阳原县| 莲花县| 聂荣县| 繁峙县| 阳西县| 丹阳市| 屯昌县| 安塞县| 洞头县| 怀化市| 乌什县| 山东| 曲麻莱县| 大城县| 明溪县| 宣城市| 滨海县| 高唐县| 张家港市| 壤塘县|