private void setMoreRing() { String[] menustring = new String[] {"设为来电铃声", "设为提示音", "设为闹铃声"}; ListView menulist = new ListView(context); menulist.setCacheColorHint(Color.TRANSPARENT); menulist.setDividerHeight(1); menulist.setAdapter( new ArrayAdapter<String>(context, R.layout.dialog_menu_item, R.id.text1, menustring)); menulist.setLayoutParams( new LayoutParams(MainActivity.getScreen(context)[0] / 2, LayoutParams.WRAP_CONTENT)); final CustomDialog xfdialog = new CustomDialog.Builder(context).setTitle("您要将文件处理为:").setView(menulist).create(); xfdialog.show(); menulist.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { xfdialog.cancel(); xfdialog.dismiss(); if (position == 0) { MainActivity.this.setMyRingtone(); } // 设为来电铃声 else if (position == 1) { MainActivity.this.setMyNotification(); } else if (position == 2) { MainActivity.this.setMyAlarm(); } } }); }
public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = 0; listView.setLayoutParams(params); listView.requestLayout(); return; } int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom(); int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST); for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if (listItem != null) { listItem.setLayoutParams( new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); }
private void restrictDamagesListHeight() { if (adapter.getCount() < 5) { View item = adapter.getView(0, null, damagesListView); item.measure(0, 0); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( getResources().getDimensionPixelSize(R.dimen.component_standard_size), (int) (5.5 * item.getMeasuredHeight())); damagesListView.setLayoutParams(params); } if (adapter.getCount() >= 5) { // show no more than 8 damages, and not less than 5 int showNb = Math.min(adapter.getCount(), 5); View item = adapter.getView(0, null, damagesListView); item.measure(0, 0); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( getResources().getDimensionPixelSize(R.dimen.component_standard_size), (int) ((showNb + 0.5) * item.getMeasuredHeight())); damagesListView.setLayoutParams(params); } damagesListView.requestLayout(); damagesListView.getParent().requestLayout(); }
private View getPopupWindowLayout() { if (contentView == null) { if (lAdapter != null) { contentView = new ListView(context); contentView.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); contentView.setCacheColorHint(Color.argb(0, 0, 0, 0)); if (backgroundId != 0) { contentView.setBackgroundResource(backgroundId); } else { contentView.setBackgroundColor(Color.argb(0, 0, 0, 0)); } contentView.setPadding( contentPadding[0], contentPadding[1], contentPadding[2], contentPadding[3]); contentView.setFadingEdgeLength(0); if (divider == null) { contentView.setDivider(null); contentView.setDividerHeight(0); } else { contentView.setDivider(divider); contentView.setDividerHeight(dividerHeight); } contentView.setAdapter(lAdapter); } } return contentView; }
public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) return; int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED); int totalHeight = 0; View view = null; for (int i = 0; i < listAdapter.getCount(); i++) { view = listAdapter.getView(i, view, listView); if (i == 0) view.setLayoutParams( new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.MATCH_PARENT)); view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += view.getMeasuredHeight() + 20; } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
private int measureHeight() { // 获取ListView对应的Adapter ListAdapter adapter = mListView.getAdapter(); if (null == adapter) { return 0; } int totalHeight = 0; for (int i = 0, len = adapter.getCount(); i < len; i++) { View item = adapter.getView(i, null, mListView); if (null == item) continue; // 计算子项View 的宽高 item.measure(0, 0); // 统计所有子项的总高度 totalHeight += item.getMeasuredHeight(); } ViewGroup.LayoutParams params = mListView.getLayoutParams(); if (null == params) { params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } // 最后得到整个ListView完整显示需要的高度 params.height = totalHeight + (mListView.getDividerHeight() * (adapter.getCount() - 1)); mListView.setLayoutParams(params); return params.height; }
public static void setListViewHeightBasedOnChildren(ListView listView, boolean mIsFlag) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; int moneHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 计算子项View 的宽高 totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 // sysout.println("----------高度----------"+listItem.getMeasuredHeight()); // if(i==len-1 && (mIsFlag)){ //评论特殊处理 // moneHeight = listItem.getMeasuredHeight()/2+10; // } } ViewGroup.LayoutParams params = listView.getLayoutParams(); totalHeight = moneHeight + totalHeight; params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); // sysout.println("-----总高度----------------"+params.height); // listView.getDividerHeight()获取子项间分隔符占用的高度 // params.height最后得到整个ListView完整显示需要的高度 listView.setLayoutParams(params); // sysout.println("---------ListView高度----------"+listView.getLayoutParams().height); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_enter_company); lv = (ListView) findViewById(R.id.listView1); linkPersonEditText = (EditText) findViewById(R.id.editText1); // set up the items fields = getResources().getStringArray(R.array.EnterCompanyFields); if (savedInstanceState != null) { restoreState(savedInstanceState); } else { Intent i = getIntent(); company = (Company) i.getSerializableExtra("company"); } epla = new EnterCompanyListAdapter(this, fields, company); lv.setAdapter(epla); // properties = new HashMap<String, String>(); if (company == null) company = new Company(); Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, (int) (height * .65), 0); lv.setLayoutParams(lp); }
public void setListViewHeightBasedOnChildren(ListView listView, int offset) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 计算子项View 的宽高 // listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), // MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); // listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.AT_MOST), // MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + offset; // listView.getDividerHeight()获取子项间分隔符占用的高度 // params.height最后得到整个ListView完整显示需要的高度 listView.setLayoutParams(params); }
public void fixListViewHeight(ListView listView) { // 如果没有设置数据适配器,则ListView没有子项,返回。 ListAdapter listAdapter = listView.getAdapter(); int totalHeight = 0; if (listAdapter == null) { return; } for (int index = 0, len = listAdapter.getCount(); index < len; index++) { View listViewItem = listAdapter.getView(index, null, listView); // 计算子项View 的宽高 // listViewItem.measure(0, 0); listViewItem.measure( View.MeasureSpec.makeMeasureSpec( getResources().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY), 0); // 计算所有子项的高度和 int height = 0; if (listViewItem.getHeight() > listViewItem.getMeasuredHeight()) height = listViewItem.getHeight(); else height = listViewItem.getMeasuredHeight(); totalHeight += height; } ViewGroup.LayoutParams params = listView.getLayoutParams(); // listView.getDividerHeight()获取子项间分隔符的高度 // params.height设置ListView完全显示需要的高度 params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
private ListView createOverflowListView() { final Context context = mContentView.getContext(); final ListView overflowListView = new ListView(context); overflowListView.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); overflowListView.setDivider(null); overflowListView.setDividerHeight(0); final ArrayAdapter overflowListViewAdapter = new ArrayAdapter<MenuItem>(context, 0) { @Override public View getView(int position, View convertView, ViewGroup parent) { TextView menuButton; if (convertView != null) { menuButton = (TextView) convertView; } else { menuButton = createOverflowMenuItemButton(context); } MenuItem menuItem = getItem(position); menuButton.setText(menuItem.getTitle()); menuButton.setContentDescription(menuItem.getTitle()); menuButton.setMinimumWidth(mOverflowWidth); return menuButton; } }; overflowListView.setAdapter(overflowListViewAdapter); return overflowListView; }
/** * Sets ListView height dynamically based on the height of the items. * * @param listView to be resized * @return true if the listView is successfully resized, false otherwise */ public static boolean setListViewHeightBasedOnItems(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter != null) { int numberOfItems = listAdapter.getCount(); // Get total height of all items. int totalItemsHeight = 0; for (int itemPos = 0; itemPos < numberOfItems; itemPos++) { View item = listAdapter.getView(itemPos, null, listView); item.measure(0, 0); totalItemsHeight += item.getMeasuredHeight(); } // Get total height of all item dividers. int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1); // Set list height. ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalItemsHeight + totalDividersHeight; listView.setLayoutParams(params); listView.requestLayout(); return true; } else { return false; } }
private void initView() { mContext = getContext(); mListView = new ListView(mContext); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); mListView.setLayoutParams(params); addView(mListView); }
/** * method to populate one menu list * * @param menuList * @param listTitle * @param menuNames * @param menuIcons */ private ListView setupList(String listTitle, String[] menuNames, int[] menuIcons) { SimpleAdapter menuAdapter = new SimpleAdapter(getActivity()); for (int i = 0; i < menuNames.length; i++) { listObject menuObject = new listObject(); menuObject.setText(menuNames[i]); menuObject.setResID(menuIcons[i]); menuAdapter.add(menuObject); } LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); View headerview = layoutInflater.inflate(R.layout.sliding_menu_list_header, null); TextView listHeaderName = (TextView) headerview.findViewById(R.id.sliding_menu_list_name); listHeaderName.setTypeface(font); listHeaderName.setText(listTitle); ListView menuList = new ListView(parentActivity); menuList.setLayoutParams(params); int sizeInPx = 10; float scale = getResources().getDisplayMetrics().density; int sizeInDp = (int) (sizeInPx * scale + 0.5f); menuList.setPadding(sizeInDp, 0, sizeInDp, 0); // menuList.setPadding(R.dimen.list_padding, 0, R.dimen.list_padding, 0); menuList.addHeaderView(headerview); menuList.setAdapter(menuAdapter); listRoot.addView(menuList, params); return menuList; }
public static void makeListViewFullSize(ListView lv, int itemHeight) { int itemCount = lv.getAdapter().getCount(); int divider = lv.getDividerHeight(); int height = (itemHeight + divider) * itemCount; ViewGroup.LayoutParams lp = lv.getLayoutParams(); lp.height = height; lv.setLayoutParams(lp); }
public void setSize() { int valPxList = densityToPixel(getDeviceDensity()); mListView = (ListView) v.findViewById(android.R.id.list); LayoutParams lpArticle = (LayoutParams) mListView.getLayoutParams(); lpArticle.height = noOfItem * valPxList; mListView.setLayoutParams(lpArticle); }
public void onClick(View paramView) { switch (paramView.getId()) { default: super.onClick(paramView); return; case 2131361873: case 2131362054: this.mProfileHeaderAdapter.onProfileHeaderClick(paramView, this); return; case 2131362056: case 2131362058: this.mProfileHeaderAdapter.onProfileHeaderClick(paramView, this); changeTab((Button)paramView); return; case 2131362051: onFollowersClicked(); return; case 2131362053: onFollowingClicked(); return; case 2131361949: this.mAppController.acceptFollowRequest(this.mAppController.getActiveSession(), this.mUserId); return; case 2131361946: this.mAppController.acceptRejectRequest(this.mAppController.getActiveSession(), this.mUserId); return; case 2131362057: } if (this.mHideProfileReposts); for (int i = 2131624352; ; i = 2131624148) { String str = getString(i); ArrayAdapter localArrayAdapter = new ArrayAdapter(getActivity(), 2130903120, 2131361987, new String[] { str }); ListView localListView = new ListView(getActivity()); FrameLayout.LayoutParams localLayoutParams = new FrameLayout.LayoutParams(-1, -2); localLayoutParams.topMargin = getResources().getDimensionPixelSize(2131427412); localListView.setPadding(0, getResources().getDimensionPixelSize(2131427412), 0, 0); localListView.setLayoutParams(localLayoutParams); localListView.setAdapter(localArrayAdapter); localListView.setSelector(new ColorDrawable(getResources().getColor(17170445))); localListView.setOnItemClickListener(this); localListView.setDividerHeight(0); if (getView() == null) break; TabButton localTabButton = (TabButton)getView().findViewById(2131362056); if ((this.mPopupWindow == null) || (this.mPopupWindow.isShowing()) || (localTabButton == null)) break; this.mPopupWindow.setFocusable(true); this.mPopupWindow.setContentView(localListView); this.mPopupWindow.setWidth(localTabButton.getMeasuredWidth()); this.mPopupWindow.setHeight(-2); this.mPopupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(2131296358))); this.mPopupWindow.showAsDropDown(localTabButton, 0, 0); return; } }
public static void setListViewHeight(Context context, ListView listView, int top, int bottom) { DisplayMetrics metrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics); int height = metrics.heightPixels; android.view.ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); layoutParams.height = top * height / bottom; listView.setLayoutParams(layoutParams); }
public TiListView(TiViewProxy proxy, Activity activity) { super(proxy); // initializing variables sections = new ArrayList<ListSectionProxy>(); itemTypeCount = new AtomicInteger(2); templatesByBinding = new HashMap<String, TiListViewTemplate>(); defaultTemplateBinding = UIModule.LIST_ITEM_TEMPLATE_DEFAULT; caseInsensitive = true; // handling marker HashMap<String, Integer> preloadMarker = ((ListViewProxy) proxy).getPreloadMarker(); if (preloadMarker != null) { setMarker(preloadMarker); } else { resetMarker(); } // initializing listView and adapter ListViewWrapper wrapper = new ListViewWrapper(activity); wrapper.setFocusable(false); wrapper.setFocusableInTouchMode(false); listView = new ListView(activity); listView.setLayoutParams( new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); wrapper.addView(listView); adapter = new TiBaseAdapter(activity); // init inflater if (inflater == null) { inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } listView.setCacheColorHint(Color.TRANSPARENT); getLayoutParams().autoFillsHeight = true; getLayoutParams().autoFillsWidth = true; listView.setFocusable(true); listView.setFocusableInTouchMode(true); listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); try { headerFooterId = TiRHelper.getResource("layout.titanium_ui_list_header_or_footer"); listItemId = TiRHelper.getResource("layout.titanium_ui_list_item"); titleId = TiRHelper.getResource("id.titanium_ui_list_header_or_footer_title"); listContentId = TiRHelper.getResource("id.titanium_ui_list_item_content"); isCheck = TiRHelper.getResource("drawable.btn_check_buttonless_on_64"); hasChild = TiRHelper.getResource("drawable.btn_more_64"); disclosure = TiRHelper.getResource("drawable.disclosure_64"); accessory = TiRHelper.getResource("id.titanium_ui_list_item_accessoryType"); } catch (ResourceNotFoundException e) { Log.e(TAG, "XML resources could not be found!!!", Log.DEBUG_MODE); } this.wrapper = wrapper; setNativeView(wrapper); }
private void setListViewHeight() { int itemHeight = getEstimatedToolbarHeight(mContentView.getContext()); int height = mListView.getAdapter().getCount() * itemHeight; int maxHeight = mContentView .getContext() .getResources() .getDimensionPixelSize(R.dimen.floating_toolbar_minimum_overflow_height); ViewGroup.LayoutParams params = mListView.getLayoutParams(); params.height = Math.min(height, maxHeight); mListView.setLayoutParams(params); }
/** * Sets the list to be used in this pull to refresh container. * @param list the list to use */ public void setList(ListView list) { if (mList != null) { mContainer.removeView(mList); } mList = list; if (mList.getParent() != null) { ViewGroup parent = (ViewGroup) mList.getParent(); parent.removeView(mList); } mList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, getHeight())); mContainer.addView(mList, 1); }
@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // We want the list to fill up the entire container. // FIXME This does not yet work perfectly. When you first show this View, the ListView does not fill the container. if (mList.getHeight() != getHeight()) { mList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, getHeight())); mNeedScroll = true; } if (mContainer.getHeight() != (getHeight() + mHeaderContainer.getHeight())) { mContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, getHeight() + mHeaderContainer.getHeight())); mNeedScroll = true; } }
public static void setListViewHeight(ListView mListView, BaseAdapter adapter) { adapter = (BaseAdapter) mListView.getAdapter(); if (adapter == null) { return; } int mTotalHeight = 0; for (int i = 0; i < adapter.getCount(); i++) { View mListItem = adapter.getView(i, null, mListView); mListItem.measure(0, 0); mTotalHeight += mListItem.getMeasuredHeight(); } ViewGroup.LayoutParams mParams = mListView.getLayoutParams(); mParams.height = mTotalHeight + (mListView.getDividerHeight() * (adapter.getCount() - 1)); mListView.setLayoutParams(mParams); }
private void init(Context context, AttributeSet attrs) { mListView = new ListView(context, attrs); mListView.setPadding(0, 0, 0, 0); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); params.bottomMargin = 0; params.topMargin = 0; params.leftMargin = 0; params.rightMargin = 0; mListView.setLayoutParams(params); mListView.setId(android.R.id.list); mListView.setOnScrollListener(mOnScrollListener); addView(mListView); }
@SuppressWarnings("deprecation") private void inflateTitleBar(ViewGroup view) { ViewStub stub = (ViewStub) view.findViewById(R.id.com_facebook_picker_title_bar_stub); if (stub != null) { View titleBar = stub.inflate(); final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); layoutParams.addRule(RelativeLayout.BELOW, R.id.com_facebook_picker_title_bar); listView.setLayoutParams(layoutParams); if (titleBarBackground != null) { titleBar.setBackgroundDrawable(titleBarBackground); } doneButton = (Button) view.findViewById(R.id.com_facebook_picker_done_button); if (doneButton != null) { doneButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { logAppEvents(true); appEventsLogged = true; if (onDoneButtonClickedListener != null) { onDoneButtonClickedListener.onDoneButtonClicked(PickerFragment.this); } } }); if (getDoneButtonText() != null) { doneButton.setText(getDoneButtonText()); } if (doneButtonBackground != null) { doneButton.setBackgroundDrawable(doneButtonBackground); } } titleTextView = (TextView) view.findViewById(R.id.com_facebook_picker_title); if (titleTextView != null) { if (getTitleText() != null) { titleTextView.setText(getTitleText()); } } } }
/** 根据ListView的所有子项的高度设置其高度 */ public static void setListViewHeightByAllChildrenViewHeight(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter != null) { int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10); listView.setLayoutParams(params); } }
/** * Populate Dynamic Listview * * @param listView */ public void setListView(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 += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); }
// 设置listview 高度 public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter; // 取得listview绑定的适配器 if (listView.getAdapter() == null) { return; } listAdapter = listView.getAdapter(); ViewGroup.LayoutParams params = listView.getLayoutParams(); // 取得listview所在布局的参数 params.height = PublicMethod.getPxInt(40, this) * (listAdapter.getCount()); listView.setLayoutParams(params); // 改变listview所在布局的参数 }
/** * 计算listview的高度,但子ListView每个Item必须是LinearLayout * * @param listView */ public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight() + 5; } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
public static LinearLayout BuildGetMoreApps(final Activity activity) { ParseJSONAds parseJSONAds = new ParseJSONAds( activity, activity.getResources().getConfiguration().locale.getCountry(), "android"); ArrayList<AdItem> adItems = parseJSONAds.getAds(); ListView listView = new ListView(activity); FlexibleRowAdapter adapter = new FlexibleRowAdapter(activity, adItems, activity.getResources()); listView.setAdapter(adapter); listView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (position > 0) { // check internet if (!UIUtils.isOnline(activity)) { UIUtils.showAlertErrorNoInternet(activity, false); return; } Object urlObject = view.findViewById(R.id.title).getTag(); if (urlObject != null) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(urlObject.toString())); activity.startActivity(i); } } } }); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); layoutParams.setMargins(0, 0, 0, 50); listView.setLayoutParams(layoutParams); LinearLayout linearLayout = new LinearLayout(activity); if (android.os.Build.VERSION.SDK_INT >= 17) { // call something for API Level 11+ linearLayout.setLayoutDirection(LinearLayout.LAYOUT_DIRECTION_LTR); } // linearLayout.addView(progressBar1); linearLayout.addView(listView); return linearLayout; }