下面列出了android.widget.ListView#setLayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected View createView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
// 不显示默认标题
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
mCommonFrameLayout = new CommonFrameLayout(getContext());
mToolbar = mCommonFrameLayout.getTitleView();
mMoreButton = mToolbar.addMoreImageButton();
LinearLayout content = LayoutUtil.newCommonLayout(getContext());
mListView = new ListView(getContext());
mListView.setCacheColorHint(0x00000000);
mListView.setDividerHeight(0);
mListView.setLayoutParams(LayoutUtil.newMatchFrameLayoutParams());
content.addView(mListView);
mCommonFrameLayout.setContent(content);
return mCommonFrameLayout;
}
private void initListView() {
mListView = new ListView(mContext);
mListView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT));
mListView.setDivider(null);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (mOnItemClickListener != null) {
MenuItem menuItem = null;
if (mMenuItemList != null) {
menuItem = mMenuItemList.get(position);
}
mOnItemClickListener.onItemClick(adapterView, view, position, id, menuItem);
}
mPopupWindow.dismiss();
}
});
mContainerLayout.addView(mListView);
}
/**
* 计算listview的高度
* @param listView
* @return
*/
public static int setListViewHeightBasedOnChildren(final ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return 0;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
// listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
final ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// MyApplication.getApplication().runOnUiThread(new Runnable() {
// @Override
// public void run() {
listView.setLayoutParams(params);
// }
// });
return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
}
/** 动态改变listView的高度 */
public static void setExpandableListViewHeightBasedOnChildren(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 = 80 * (listAdapter.getCount() - 1);
// params.height = 80 * (listAdapter.getCount());
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//((ViewGroup.MarginLayoutParams) params).setMargins(0, 0, 0, 0);
listView.setLayoutParams(params);
}
public static void setListViewHeightBasedOnChildren(ListView listView) {
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = getListViewHeightBasedOnChildren(listView);
listView.setLayoutParams(params);
System.out.println("params.height:" + params.height);
LinearLayout linearLayout = (LinearLayout) listView.getParent();
// android.widget.LinearLayout.LayoutParams params2 = new
// android.widget.LinearLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, params.height);
//
// params2.setMargins(10, 0, 10, 10);
linearLayout.setLayoutParams(params);
}
/**
* @param
* @return void 返回类型
* @Title setListViewHeightBasedOnChildren
* @Description 根据children高度和多少,重新设置listview的高度
*/
public static void setListViewHeightBasedOnChildren(ListView listView,
int attHeight) {
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();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1))
+ attHeight;
listView.setLayoutParams(params);
}
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
Log.w("HEIGHT" + i, String.valueOf(totalHeight));
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
/**
* 设置ListView的高度,只有高度固定,才不会和外层的ScrollView产生冲突。
* ScrollView内部嵌入ListView会引起ListView显示不全(只显示一行半)或无法滚动ListView。
*
* @param listView
*/
private static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
if (listAdapter.getCount() == 0) {
listView.setVisibility(View.GONE);
return;
} else {
listView.setVisibility(View.VISIBLE);
}
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);
}
public void setListViewHeightBasedOnChildren(ListView listView, int childCount) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < childCount; i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
int px = listItem.getMeasuredHeight();
totalHeight += px * 1.2;
}
Log.d(TAG, String.format("total height: %d", totalHeight));
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (5 - 1));
listView.setLayoutParams(params);
}
/**
* @param listView
* @author sunglasses
* @category 计算listview高度
*/
public static void setListViewHeightBasedOnChildren(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);
}
/**
* 初始化已下载列表
*/
public void initDownloadedList() {
mDownLoadedList = (ListView) LayoutInflater.from(
OfflineMapActivity_Old.this).inflate(
R.layout.offline_downloaded_list, null);
android.widget.AbsListView.LayoutParams params = new android.widget.AbsListView.LayoutParams(
android.widget.AbsListView.LayoutParams.MATCH_PARENT,
android.widget.AbsListView.LayoutParams.WRAP_CONTENT);
mDownLoadedList.setLayoutParams(params);
mDownloadedAdapter = new OfflineDownloadedAdapter(this, amapManager);
mDownLoadedList.setAdapter(mDownloadedAdapter);
}
public static boolean setListViewHeightBasedOnItems(ListView listView) {
DrawerItemCustomAdapter listAdapter = (DrawerItemCustomAdapter) 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;
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView = (ListView) getView().findViewById(android.R.id.list);
int marginTop = getActivity().getResources().getDimensionPixelSize(
R.dimen.search_engine_list_margin_top);
MarginLayoutParams layoutParams = (MarginLayoutParams) mListView.getLayoutParams();
layoutParams.setMargins(0, marginTop, 0, 0);
mListView.setLayoutParams(layoutParams);
mListView.setAdapter(mSearchEngineAdapter);
mListView.setDivider(null);
}
@Override
public View onCreateView() {
LinearLayout ll_container = new LinearLayout(mContext);
ll_container.setOrientation(LinearLayout.VERTICAL);
/** title */
mTvTitle = new TextView(mContext);
mTvTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
mTvTitle.setSingleLine(true);
mTvTitle.setPadding(dp2px(18), dp2px(10), 0, dp2px(10));
ll_container.addView(mTvTitle);
/** listview */
mLv = new ListView(mContext);
mLv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
mLv.setCacheColorHint(Color.TRANSPARENT);
mLv.setFadingEdgeLength(0);
mLv.setVerticalScrollBarEnabled(false);
mLv.setSelector(new ColorDrawable(Color.TRANSPARENT));
ll_container.addView(mLv);
return ll_container;
}
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewGroup view = (ViewGroup) getView();
if (showSearchBox) {
ViewStub stub = (ViewStub) view.findViewById(R.id.com_facebook_placepickerfragment_search_box_stub);
if (stub != null) {
searchBox = (EditText) stub.inflate();
// Put the list under the search box
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.BELOW, R.id.search_box);
ListView listView = (ListView) view.findViewById(R.id.com_facebook_picker_list_view);
listView.setLayoutParams(layoutParams);
// If we need to, put the search box under the title bar.
if (view.findViewById(R.id.com_facebook_picker_title_bar) != null) {
layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, R.id.com_facebook_picker_title_bar);
searchBox.setLayoutParams(layoutParams);
}
searchBox.addTextChangedListener(new SearchTextWatcher());
if (!TextUtils.isEmpty(searchText)) {
searchBox.setText(searchText);
}
}
}
}
public popupList(Context context, int width, int height) {
super(context, width, height);
setBackgroundColor(0x66000000);
sl = new FMlyt(getContext(), Ui.cd.getHt(200), Ui.cd.getHt(300)){};
sl.setBackgroundColor(0x00000000);
sl.InCenter(width,height);
rs = new radiusSqure(sl.width - Ui.cd.getHt(4),sl.height - Ui.cd.getHt(4),0,0, Ui.cd.getHt(13));
rs.setColor(popupBack.Color0);
// addShape(rs);
rb = new radiusBorder(sl.width,sl.height,0,0, Ui.cd.getHt(2), Ui.cd.getHt(13));
rb.setColor(savebtnRingColor.Color0);
sl.addShape(rb);
presetAdapter data;
listview = new ListView(getContext()){
@Override
protected void onDraw(Canvas canvas) {
canvas.clipPath(rs.S0);
super.onDraw(canvas);
}
};
listview.setLayoutParams(new FrameLayout.LayoutParams( sl.width - Ui.cd.getHt(4), sl.height - Ui.cd.getHt(4)));
listview.setDivider(null);
listview.setX(Ui.cd.getHt(2));
listview.setY(Ui.cd.getHt(2));
listview.setBackgroundResource(0);
//listview.setBackgroundColor(com.shape.home.slider.backgroundImg.Color0);
data = new presetAdapter(){
@Override
public int getHeigtht() {
return sl.width - Ui.cd.getHt(4);
}
@Override
public void onReload(){
if(listview != null){
Ui.bk.back();
popupList.this.onReload();
}
}
};
listview.setAdapter(data);
sl.addView(listview);
addView(sl);
}
@Override
public View onCreateView() {
LinearLayout ll_container = new LinearLayout(mContext);
ll_container.setOrientation(LinearLayout.VERTICAL);
ll_container.setBackgroundColor(Color.TRANSPARENT);
/** title */
mTvTitle = new TextView(mContext);
mTvTitle.setGravity(Gravity.CENTER);
mTvTitle.setPadding(dp2px(10), dp2px(5), dp2px(10), dp2px(5));
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.topMargin = dp2px(20);
ll_container.addView(mTvTitle, params);
/** title underline */
mVLineTitle = new View(mContext);
ll_container.addView(mVLineTitle);
/** listview */
mLv = new ListView(mContext);
mLv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));
mLv.setCacheColorHint(Color.TRANSPARENT);
mLv.setFadingEdgeLength(0);
mLv.setVerticalScrollBarEnabled(false);
mLv.setSelector(new ColorDrawable(Color.TRANSPARENT));
ll_container.addView(mLv);
/** mCancel btn */
mTvCancel = new TextView(mContext);
mTvCancel.setGravity(Gravity.CENTER);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.topMargin = dp2px(7);
lp.bottomMargin = dp2px(7);
mTvCancel.setLayoutParams(lp);
ll_container.addView(mTvCancel);
return ll_container;
}
public void init(LinearLayout linearLayout1, CustomLinearLayout layout){
ListView listView = new ListView(this);
listView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
listView.setOnItemClickListener(this);
InfoAdapter adapter = new InfoAdapter(this);
adapter.setItems(getList());
listView.setAdapter(adapter);
linearLayout1.addView(listView);
String[] colors = new String[]{"#417BC7", "#6FA13E", "#DB9548", "#C8382E", "#753B94"};
// for (int i = 0; i < 10; i++){
// CustomLabelTextView customLabelTextView = new CustomLabelTextView(
// this,
// "测试文字",
// "test"+i,
// "#58575c",
// CustomLabelTextView.randomColorString(colors));
// layout.addView(customLabelTextView);
// }
initModel(layout);
for (VersionEnum versionEnum : VersionEnum.values()){
CustomLabelTextView customLabelTextView = new CustomLabelTextView(
this,
versionEnum.isSupported() ? "已支持版本" : "不支持版本",
versionEnum.getVersion(),
"#58575c",
CustomLabelTextView.randomColorString(colors));
layout.addView(customLabelTextView);
}
TextView tv_left = new TextView(this);
tv_left.setText("测试");
tv_left.setTextColor(Color.WHITE);
tv_left.setClickable(true);
tv_left.setPadding(dp2px(10), dp2px(2), dp2px(10), dp2px(2));
GradientDrawable drawable1 = ViewUtil.createGradientDrawableRadius(2, new float[]{14, 14, 0, 0, 0, 0, 14, 14}, Color.MAGENTA);
drawable1.setColor(ViewUtil.createColorStateList(
Color.parseColor("#58575c"),
Color.parseColor("#ae58575c"),
Color.parseColor("#58575c"),
Color.parseColor("#a158575c")));
tv_left.setBackground(drawable1);
LinearLayout.LayoutParams tv_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, dp2px(24));
tv_left.setLayoutParams(tv_params);
layout.addView(tv_left);
PlusButton plusButton = new PlusButton(this);
plusButton.setText("测试按钮");
plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击事件", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, TestActivity.class);
MainActivity.this.startActivity(intent);
}
});
layout.addView(plusButton);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-4147454460675251/3837655321");
adView.setAdSize(AdSize.BANNER);
adView.setAdListener(adListener);
RelativeLayout adLayout = (RelativeLayout)findViewById(R.id.main_page_adview_layout);
adLayout.addView(adView);
RelativeLayout.LayoutParams adViewlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
adViewlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adViewlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
adView.setLayoutParams(adViewlp);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("04E14595EE84A505616E50A99B15252D")
.build();
adView.loadAd(request);
System.out.println("---------------------- istestdevice: "+request.isTestDevice(this));
mContext = this;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics( dm );
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
final TextView headerText = (TextView)findViewById(R.id.mainHeaderText);
RelativeLayout.LayoutParams headerTextParams = (RelativeLayout.LayoutParams)headerText.getLayoutParams();
headerTextParams.leftMargin = screenHeight/8;
headerText.setLayoutParams(headerTextParams);
headerText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, (float)screenHeight/35);
headerText.setText("Face Recognition");
headerText.setTextColor(Color.LTGRAY);
headerText.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
headerText.setTypeface(null, Typeface.BOLD);
ImageView headerIcon = (ImageView)findViewById(R.id.mainHeaderIcon);
RelativeLayout.LayoutParams iconLParams = new RelativeLayout.LayoutParams(screenHeight/11, screenHeight/11);
iconLParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT | RelativeLayout.CENTER_VERTICAL);
iconLParams.leftMargin = screenHeight/80;
headerIcon.setLayoutParams(iconLParams);
ListView listView = (ListView)findViewById(R.id.mainListView);
ListAdapter listAdapter = new ListAdapter();
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(itemClickListener);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)listView.getLayoutParams();
params.leftMargin = screenWidth/10;
params.rightMargin = screenWidth/10;
params.topMargin = screenHeight/12;
listView.setLayoutParams(params);
listView.setVerticalScrollBarEnabled(false);
}
private void drawInnerViews(Context context, AttributeSet attrs){
float density = context.getResources().getDisplayMetrics().density;
int value16 = (int)(16*density);
int value10 = (int)(10*density);
int value40 = (int)(40*density);
LayoutParams mainLayoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mainLayoutParams.setMargins(value16,value16,value16,value16);
setLayoutParams(mainLayoutParams);
//creation & addition of webview
webview = new WebView(context, attrs);
webview.setId(android.R.id.content);
webview.setLayoutParams(
new LayoutBuilder()
.addWidth(LayoutParams.MATCH_PARENT)
.addHeight(LayoutParams.WRAP_CONTENT)
.setMargin(value10,value40,0,0)
.create()
);
webview.getSettings();
webview.setBackgroundColor(Color.argb(0,0,0,0));
addView(webview);
//creation of list view
listView = new ListView(context, attrs);
listView.setId(android.R.id.list);
listView.setLayoutParams(
new LayoutBuilder()
.addWidth(LayoutParams.MATCH_PARENT)
.addHeight(LayoutParams.WRAP_CONTENT)
.setMargin(0,value10,0,0)
.addRule(BELOW, webview.getId() )
.create()
);
addView(listView );
//creation & addition of editText
editTv = new AppCompatEditText(context, attrs);
editTv.setVisibility(GONE);
editTv.setId(android.R.id.text1);
editTv.setLayoutParams(
new LayoutBuilder()
.addWidth(LayoutParams.MATCH_PARENT)
.addHeight(LayoutParams.WRAP_CONTENT)
.setMargin(value10, value10, 0, 0)
.addRule(BELOW, webview.getId())
.create()
);
editTv.setInputType(InputType.TYPE_CLASS_TEXT);
editTv.setImeOptions(EditorInfo.IME_ACTION_DONE);
addView(editTv );
}