前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >popupwindow和listview

popupwindow和listview

作者头像
xiangzhihong
发布2018-01-30 14:34:53
1.6K0
发布2018-01-30 14:34:53
举报
文章被收录于专栏:向治洪向治洪

在使用PopupWindow的时候,有一个不好的地方就是不太好设置弹出窗体的大小。如果指定绝对大小,那么对于不同分辨率不同尺寸的手机来说,显示出来效果会不同,从而导致用户体验不佳。

为了达到PopupWindow能够自适配布局大小,可以在设置长宽时候指定:

代码语言:js
复制
 popupWindow.setWidth(LayoutParams.WRAP_CONTENT);    
 popupWindow.setHeight(LayoutParams.WRAP_CONTENT);   

下面我就来具体讲解一下在PopupWindow中使用ListView的方法。

首先贴出的是main.xml

代码语言:html
复制
 <?xml version="1.0" encoding="utf-8"?> 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" android:layout_width="fill_parent" 
  android:layout_height="fill_parent"> 
  
  <Button android:id="@+id/button" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="弹出popupWindow" /> 
  
 </LinearLayout> 

然后贴出的是PopupWindow中显示的listview_demo.xml

代码语言:html
复制
 <?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"> 
  
  <ListView android:id="@+id/listview" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" /> 
  
 </LinearLayout> 

再贴出的是listview显示的每一项item.xml

代码语言:html
复制
 <?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"> 
  
  <TextView android:id="@+id/item" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textSize="18sp" /> 
  
 </LinearLayout> 

最后贴出的是java代码PopupWindowDemoActivity.java

代码语言:java
复制
 package xmu.zgy;  
  
 import java.util.ArrayList;  
 import java.util.HashMap;  
 import java.util.List;  
 import java.util.Map;  
  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.ViewGroup.LayoutParams;  
 import android.widget.Button;  
 import android.widget.ListView;  
 import android.widget.PopupWindow;  
 import android.widget.SimpleAdapter;  
  
 /** 
  *  
  * @author yulongfei 
  * @blog blog.csdn.net/zgyulongfei 
  * 
  */ 
 public class PopupWindowDemoActivity extends Activity {  
  
  private Button button;  
  private PopupWindow popupWindow;  
  private ListView listView;  
  
  @Override 
  public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
  
         initControls();  
     }  
  
  private void initControls() {  
         LayoutInflater inflater = LayoutInflater.from(this);  
         View view = inflater.inflate(R.layout.listview_demo, null);  
  
         SimpleAdapter adapter = new SimpleAdapter(this, getData(),   
                 R.layout.item,  
  new String[] { "text" },  
  new int[] { R.id.item });  
         listView = (ListView) view.findViewById(R.id.listview);  
         listView.setAdapter(adapter);  
  
  //自适配长、框设置 
         popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,  
                 LayoutParams.WRAP_CONTENT);  
         popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));  
         popupWindow.setOutsideTouchable(true);  
         popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);  
         popupWindow.update();  
         popupWindow.setTouchable(true);  
         popupWindow.setFocusable(true);  
  
         button = (Button) findViewById(R.id.button);  
         button.setOnClickListener(new OnClickListener() {  
  @Override 
  public void onClick(View v) {  
  if (!popupWindow.isShowing()) {  
                     popupWindow.showAsDropDown(button, 0, 0);  
                 }  
             }  
         });  
     }  
  
  private List<Map<String, String>> getData() {  
         List<Map<String, String>> list = new ArrayList<Map<String, String>>();  
  
         Map<String, String> map = new HashMap<String, String>();  
         map.put("text", "中国");  
         list.add(map);  
  
         map = new HashMap<String, String>();  
         map.put("text", "加油");  
         list.add(map);  
  
         map = new HashMap<String, String>();  
         map.put("text", "钓鱼岛是中国的");  
         list.add(map);  
  
         map = new HashMap<String, String>();  
         map.put("text", "!!");  
         list.add(map);  
  return list;  
     }  
  
 }  

运行结果图如下所示:

咦?不是已经设置自适应长和宽了吗?为什么显示出来的效果还是占满屏幕的宽度呢?

可以看看stackoverflow上面这个人问的问题,这个问题想必纠结了挺多人。虽然我不知道具体的原因是什么,但是我有个解决的方案,我也同时在stackoverflow上做了解答,下面我具体来说明一下。

为了让PopupWindow能够自适应ListView的内容,需要在listview_demo.xml添加一项:

代码语言:html
复制
 <?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"> 
  
  <ListView android:id="@+id/listview" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" /> 
  
  <TextView android:layout_width="wrap_content" 
  android:layout_height="0dp" 
  android:textSize="18sp" 
  android:text="钓鱼岛是中国的" /> 
 </LinearLayout> 

先看显示结果再做解释:

看到了吗?很神奇吧,popupwindow的宽度进行了自适配。

因为我在xml中加了一个TextView,然后设置了高度为0,这样他就看不到了。

最重要的步骤是我在TextView中设置了android:text="钓鱼岛是中国的",这一句是关键性的动作。

因为TextView才是自适配的砝码,要在text中写上你的listView中最长的那个字符。上述demo中,所有显示的文字{中国,加油,钓鱼岛是中国的,!!!}中”钓鱼岛是中国的“是最长的。

虽然方法不太好,但是实现了效果。如果你遇到这样的问题,可以试试这种方式。

希望本文能够帮到有需要的朋友!

点击下载本文Demo。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-01-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档