80,351
社区成员
发帖
与我相关
我的任务
分享
int list_height ;
list_height = getMsgListViewHeight(listView);
Log.v("zzw", list_height +"");
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
if (list_height > height*3/5) {
list_height = height*3/5;
}
LinearLayout.LayoutParams LayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, list_height);
((ScrollView) mDialogLayout.findViewById(R.id.sv)).setLayoutParams(LayoutParams);
private int getMsgListViewHeight(ListView mMessageCenterLv) {
int totalHeight = 0;
ListAdapter listAdapter = mMessageCenterLv.getAdapter();
if (listAdapter == null) {
return totalHeight;
}
int height = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, mMessageCenterLv);
int desiredWidth = View.MeasureSpec.makeMeasureSpec(mMessageCenterLv.getWidth(), View.MeasureSpec.AT_MOST);
listItem.measure(desiredWidth, 0);
height += (listItem.getMeasuredHeight());
}
totalHeight = height + (mMessageCenterLv.getDividerHeight() * (listAdapter.getCount() - 1));
return totalHeight;
}
public Dialog builder() {
dialog = new Dialog(mContext, R.style.MyDialogTheme);
dialog.setCancelable(mCancelable);
dialog.addContentView(mDialogLayout, new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT));
//如果是中间弹窗
if (!mIsBottomDialog) {
//如果没有设置Title
if (!mIsShowTitle) {
mDialogLayout.findViewById(R.id.title_background).setVisibility(View.GONE);
}
//如果设置显示了ListView
if (mIsShowListView) {
ListView listView = new ListView(mContext);
LinearLayout.LayoutParams listLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
listView.setLayoutParams(listLayoutParams);
listView.setAdapter(mAdapter);
int list_height = measureListViewWrongHeight(listView) * listView.getCount();
if (list_height > 500) {
list_height = 500;
}
LinearLayout.LayoutParams LayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, list_height);
((ScrollView) mDialogLayout.findViewById(R.id.sv)).setLayoutParams(LayoutParams);
setView(listView);
if (mItemClickListener != null) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mItemClickListener.onItemClick(parent, view, position, id);
dialog.dismiss();
}
});
} else {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dialog.dismiss();
}
});
}
}
//如果设置了negative区域的按钮
if (mIsShowNegativeButton) {
if (negativeButton != null) {
mDialogLayout.findViewById(R.id.negative).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeButton.onClick(v);
dialog.dismiss();
}
});
} else {
mDialogLayout.findViewById(R.id.negative).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
} else {
mDialogLayout.findViewById(R.id.negative).setVisibility(View.GONE);
mDialogLayout.findViewById(R.id.line3).setVisibility(View.GONE);
}
//如果设置了positive区域的按钮
if (mIsShowPositiveButton) {
if (positiveButton != null) {
mDialogLayout.findViewById(R.id.positive).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positiveButton.onClick(v);
dialog.dismiss();
}
});
} else {
mDialogLayout.findViewById(R.id.positive).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
} else {
mDialogLayout.findViewById(R.id.positive).setVisibility(View.GONE);
mDialogLayout.findViewById(R.id.line3).setVisibility(View.GONE);
}
mDialogLayout.findViewById(R.id.negative).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (negativeButton != null) {
negativeButton.onClick(v);
}
dialog.dismiss();
}
});
//如果有自定义的动画效果传入,就显示传入的动画效果,否则显示默认效果,另外:传入0,无动画
if (mIsHaveCustomAnim) {
if (mCustomAnim != 0) { //设置显示dialog的显示动画
dialog.getWindow().setWindowAnimations(mCustomAnim);
}
} else { //设置默认dialog的显示动画
dialog.getWindow().setWindowAnimations(R.style.DialogInAndOutAnim);
}
} else { //是底部弹窗
//如果没有设置底部弹窗标题
if (!mIsShowBottomTitle) {
mDialogLayout.findViewById(R.id.bottom_title_content).setVisibility(View.GONE);
}
//如果设置了显示ListView
if (mIsShowListView) {
ListView listView = new ListView(mContext);
LinearLayout.LayoutParams listLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
listView.setLayoutParams(listLayoutParams);
listView.setAdapter(mAdapter);
setBottomView(listView);
if (mItemClickListener != null) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mItemClickListener.onItemClick(parent, view, position, id);
dialog.dismiss();
}
});
} else {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dialog.dismiss();
}
});
}
}
//如果设置了显示底部Negative按钮
if (mIsShowBottomNegativeButton) {
if (negativeButton != null) {
mDialogLayout.findViewById(R.id.bottom_negative_content).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeButton.onClick(v);
dialog.dismiss();
}
});
} else {
mDialogLayout.findViewById(R.id.bottom_negative_content).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
} else {
mDialogLayout.findViewById(R.id.bottom_negative_content).setVisibility(View.GONE);
}
//如果有自定义的动画效果传入,就显示传入的动画效果,否则显示默认效果,另外:传入0,无动画
if (mIsHaveCustomAnim) {
if (mCustomAnim != 0) { //设置显示底部dialog的显示动画
dialog.getWindow().setWindowAnimations(mCustomAnim);
}
} else { //设置默认底部dialog的显示动画
dialog.getWindow().setWindowAnimations(R.style.BottomDialogInAndOutAnim);
}
Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.BOTTOM);
}
return dialog;
}
public MyDialog setView(View v) {
/* ((LinearLayout) mDialogLayout.findViewById(R.id.content)).removeAllViews();
//一定要进行判断,否则第二次弹出Dialog时会报异常
//异常:java.lang.IllegalStateException: The specified child already has a parent.
// You must call removeView() on the child's parent first.
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null) {
parent.removeAllViews();
}
((LinearLayout) mDialogLayout.findViewById(R.id.content)).addView(v);*/
((FrameLayout) mDialogLayout.findViewById(R.id.sv)).removeAllViews();
((FrameLayout) mDialogLayout.findViewById(R.id.sv)).addView(v);
return this;
}
//数据源,数组和List
private String[] mDataArray = {"我是 item 1", "我是 item 2", "我是 item 3", "我是 item 4"};
private List<String> mDataList = Arrays.asList(mDataArray);
//颜色值数组和List
private Integer[] mColorArray = {R.color.blue,R.color.red,R.color.yellow,R.color.green};
private List<Integer> mColorsList = Arrays.asList(mColorArray);
FrameLayout.LayoutParams LayoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,measureListViewWrongHeight(listView));
((LinearLayout) mDialogLayout.findViewById(R.id.content)).setLayoutParams(LayoutParams);
setView(listView);
// 动态改变listView的高度
public void measureListViewWrongHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += itemHeight;
}
// 减掉底部分割线的高度
int historyHeight = totalHeight
+ (listView.getDividerHeight() * listAdapter.getCount() - 1);
Log.d("Javine","wrongHeight = "+historyHeight); //输出最终ListView的高度
}
改变R.id.content的LinearLayout的android:layout_height="400dp"