现在的需求是 实现一个 弹出层,层里有gridview,gridview的高度超过 屏幕高度的0.8时则 动态设置 gridview(层)的高度宽度分别为 屏幕的0.8和 0.89.
包含了 动态获取gridview的高度,设置dialog的高宽度,以及 dialog中嵌套 gridview等 知识的运用。
代码如下:
dialogview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:background="@color/grid"
android:horizontalSpacing="1.0px"
android:listSelector="@null"
android:numColumns="2"
android:smoothScrollbar="true"
android:soundEffectsEnabled="true"
android:stretchMode="columnWidth"
android:verticalSpacing="1.0px"
/>
</LinearLayout>
注意这里margin 的属性全部置为 0dp,否则 会占用 dialog的空间,导致 点击 gridview之外的空白区域,有一部分无法响应 层的取消事件。 也就是 点击占用的这部分空白
弹出层不消失。 就达不到我们的需求。
gridview高宽度设为随内容自适应。
可滑动。
列数设为2,这个值在后面计算gridview的高度时需要用到。
styles.xml
设置dialog的样式
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->
</style>
</resources>
Activity
创建弹出层的方法如下:
public void createDialog(final List<FinalBean> list) {
View dialogView = inflater.inflate(R.layout.dialogview, null);
GridView mGridView = (GridView) dialogView.findViewById(R.id.gridview);
ClassAdapter listAdapter=new ClassAdapter(ReadWriteActivity.this, list);
mGridView.setAdapter(listAdapter);
final Dialog dialog = new Dialog(ReadWriteActivity.this, R.style.dialog);
dialog.setContentView(dialogView);
Window win = dialog.getWindow();
int totalHeight = 0;
// i每次加2,计算一次item的高度
for (int i = 0; i < listAdapter.getCount(); i +=2) {
// 获取listview的每一个item
View listItem = listAdapter.getView(i, null, mGridView);
listItem.measure(0, 0);
// 获取item的高度和
totalHeight += listItem.getMeasuredHeight();
}
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
if (totalHeight > d.getHeight() * 0.8) {
LayoutParams params = new LayoutParams();
params.height = (int) (d.getHeight() * 0.8); // 高度设置为屏幕的0.8
params.width = (int) (d.getWidth() * 0.89); // 宽度设置为屏幕的0.89
win.setAttributes(params);
}
dialog.setCanceledOnTouchOutside(true);
dialog.show();
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
mClassText.setText(list.get(position).getCbean().getCname());
cid = list.get(position).getCbean().getCid();
productList2 = list.get(position).getPlist();
sizeList2 = list.get(position).getSlist();
mProductText.setText("");
mSizeText.setText("");
mEdit.setText("");
dialog.dismiss();
}
});
}
这里用item高度相加的方法来算出gridview的高度,因为gridview在未绘制前 直接getHeight和getWidth为0.