下面列出了怎么用android.view.ViewGroup.LayoutParams的API类实例代码及写法,或者点击链接到github查看源代码。
private void showOptionDialog(final AlertDialog dialog) {
final IBinder windowToken = mKeyboardSwitcher.getMainKeyboardView().getWindowToken();
if (windowToken == null) {
return;
}
final Window window = dialog.getWindow();
final WindowManager.LayoutParams lp = window.getAttributes();
lp.token = windowToken;
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
mOptionsDialog = dialog;
dialog.show();
}
public void setActivity(Activity activity) {
super.setActivity(activity);
if (isDialogMode()) {
System.err.println("Theme classic does not support dialog mode!");
// activity.setTheme(android.R.style.Theme_Dialog);
// activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
// if (Build.VERSION.SDK_INT >= 11) {
// try {
// ReflectHelper.invokeInstanceMethod(activity, "setFinishOnTouchOutside", false);
// } catch (Throwable e) {}
// }
}
activity.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE |
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
private CloseButton createCloseButton(Activity context, boolean fullscreen, View parent) {
CloseButton closeButton = new CloseButton(context);
closeButton.setId(R.id.close_button);
RelativeLayout.LayoutParams closeLayout = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (fullscreen) {
closeLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP, dialogView.getId());
closeLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, dialogView.getId());
closeLayout.setMargins(0, SizeUtil.dp5, SizeUtil.dp5, 0);
} else {
closeLayout.addRule(RelativeLayout.ALIGN_TOP, parent.getId());
closeLayout.addRule(RelativeLayout.ALIGN_RIGHT, parent.getId());
closeLayout.setMargins(0, -SizeUtil.dp7, -SizeUtil.dp7, 0);
}
closeButton.setLayoutParams(closeLayout);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
cancel();
}
});
return closeButton;
}
@SuppressWarnings("deprecation")
private ImageView createBackgroundImageView(Context context, boolean fullscreen) {
BackgroundImageView view = new BackgroundImageView(context, fullscreen);
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
int cornerRadius;
if (!fullscreen) {
cornerRadius = SizeUtil.dp20;
} else {
cornerRadius = 0;
}
view.setImageBitmap(options.getBackgroundImage());
ShapeDrawable footerBackground = new ShapeDrawable();
footerBackground.setShape(createRoundRect(cornerRadius));
footerBackground.getPaint().setColor(options.getBackgroundColor());
if (Build.VERSION.SDK_INT >= 16) {
view.setBackground(footerBackground);
} else {
view.setBackgroundDrawable(footerBackground);
}
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
view.setLayoutParams(layoutParams);
return view;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
TextView text = new TextView(JazzyViewActivity.this);
text.setGravity(Gravity.CENTER);
text.setTextSize(30);
text.setTextColor(Color.WHITE);
text.setText("Page " + position);
text.setPadding(30, 30, 30, 30);
int bg = Color.rgb((int) Math.floor(Math.random()*128)+64,
(int) Math.floor(Math.random()*128)+64,
(int) Math.floor(Math.random()*128)+64);
text.setBackgroundColor(bg);
container.addView(text, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mJazzy.setObjectForPosition(text, position);
return text;
}
private void init(Context context) {
View view = LayoutInflater.from(context).inflate(
R.layout.dialog_act_agree, null);
dialog = new Dialog(context, R.style.Dialog_act_agree);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(view);
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM;
lp.width = LayoutParams.MATCH_PARENT;
window.setWindowAnimations(R.style.dialogWindowAnim);
view.findViewById(R.id.ll_agree).setOnClickListener(this);
view.findViewById(R.id.ll_refuse).setOnClickListener(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.found_fragment, container,
false);
cursor = (ImageView) rootView.findViewById(R.id.cursor);
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;// ��ȡ�ֱ��ʿ��
LayoutParams ps = cursor.getLayoutParams();
ps.width = screenW / 3;
cursor.setLayoutParams(ps);
t1 = (TextView) rootView.findViewById(R.id.text1);
t2 = (TextView) rootView.findViewById(R.id.text2);
t3 = (TextView) rootView.findViewById(R.id.text3);
viewPager = (ViewPager) rootView.findViewById(R.id.foundpager);
InitTextView();
InitImageView();
InitViewPager();
return rootView;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Use full screen window and translucent action bar
*/
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setBackgroundDrawable(new ColorDrawable(0xFF000000));
if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
getActionBar().setBackgroundDrawable(new ColorDrawable(0x88000000));
// Note: if you use ActionBarSherlock use here getSupportActionBar()
}
setContentView(R.layout.view_pager);
ViewPager viewPager = (ViewPager) findViewById(R.id.zoomViewPager);
viewPager.setAdapter(new SamplePagerAdapter());
// Add margin between pages (optional)
viewPager.setPageMargin((int) getResources().getDisplayMetrics().density * 10);
}
/**
* On pre show
*/
protected void preShow()
{
if (mRootView == null)
throw new IllegalStateException(
"setContentView was not called with a view to display.");
onShow();
if (mBackground == null)
mWindow.setBackgroundDrawable(new BitmapDrawable());
else
mWindow.setBackgroundDrawable(mBackground);
mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
mWindow.setTouchable(true);
mWindow.setFocusable(true);
mWindow.setOutsideTouchable(true);
mWindow.setContentView(mRootView);
}
private void setPortraitMode()
{
SlidingPaneLayout.LayoutParams params = (SlidingPaneLayout.LayoutParams) rightContainer.getLayoutParams();
params.width = KlyphDevice.getDeviceWidth();// rightContainerWidth;
// params.leftMargin = paddingLeft;
params.rightMargin = 0;
rightContainer.setLayoutParams(params);
params = (SlidingPaneLayout.LayoutParams) leftContainer.getLayoutParams();
params.width = leftContainerWidth;
params.leftMargin = 0;
params.rightMargin = 0;
leftContainer.setLayoutParams(params);
slidingPane.requestLayout();
}
private TextView addShareItemText(Drawable drawable, String str) {
if (drawable != null) {
drawable.setBounds(0, 0, dip2px(SHARE_ICON_SIZE), dip2px(SHARE_ICON_SIZE));
}
View textView = new TextView(getContext());
textView.setTextColor(-1);
textView.setCompoundDrawables(null, drawable, null, null);
textView.setTextSize(13.0f);
textView.setCompoundDrawablePadding(dip2px(8.0f));
textView.setPadding(0, dip2px(19.0f), 0, dip2px(19.0f));
textView.setGravity(1);
textView.setText(str);
LayoutParams layoutParams = new LinearLayout.LayoutParams(0, -2);
layoutParams.weight = 1.0f;
layoutParams.gravity = 1;
this.mRootView.addView(textView, layoutParams);
addHorizontalDivider();
return textView;
}
public void showChart(View v) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.layout_chart_fragment);
try {
View testChartView = findViewById(100); // TODO: ...
mainLayout.removeView(testChartView);
} catch (Exception e) {
// e.printStackTrace();
}
v.setId(100); // TODO: what's safe?
// add to layout
LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
v.setBackgroundColor(Color.rgb(238, 238, 238));
v.setLayoutParams(lparams);
if (mainLayout == null) {
// log("ERROR layout null, chart");
return;
}
mainLayout.addView(v);
}
/**
* Create a new row view.
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// create a row view
LinearLayout row = new LinearLayout(context);
row.setWeightSum(numColumns);
V[] gridItemViewHolders = newGridItemViewHolderArray(numColumns);
// inflate a grid item view for each column in the row
for (int col = 0; col < numColumns; col++) {
View gridItemView = newGridItemView(context, cursor, parent);
LinearLayout.LayoutParams lp = newGridItemLayoutParams(col);
gridItemViewHolders[col] = newGridItemViewHolder(gridItemView);
row.addView(gridItemView, lp);
}
// save the grid item view holders in the row view holder
RowViewHolder holder = new RowViewHolder(gridItemViewHolders);
row.setTag(holder);
return row;
}
private void startOverLayFriend() {
SlideInContactsLayout overlayContactsLayout = (SlideInContactsLayout) getLayoutInflater().inflate(
R.layout.overlay_contacts, null, false);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
PixelFormat.TRANSLUCENT
);
getWindowManager().addView(overlayContactsLayout, params);
TypedValue tv = new TypedValue();
int actionBarHeight =
getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true) ?
TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()) : 0;
overlayContactsLayout.start(this, Friend.lorem, actionBarHeight);
}
public MoreFloatingDialog(Context context) {
super(context, 16973840);
WindowManager windowManager = (WindowManager) context.getSystemService("window");
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
this.density = displayMetrics.density;
f.b(f.d, "-->(MoreFloatingDialog) : density = " + this.density);
LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
this.mContentView = new RelativeLayout(context);
this.mContentView.setLayoutParams(layoutParams);
this.mContentView.setBackgroundDrawable(a.a(SOURCE_FLOATING_BG, context, this.ninePatchRect));
this.mRootView = new LinearLayout(context);
layoutParams = new LinearLayout.LayoutParams(-1, -2);
this.mRootView.setLayoutParams(layoutParams);
this.mShareItems.put(SOURCE_QQ_BG, addShareItemText(a.a(SOURCE_QQ_BG, getContext(), this.ninePatchRect), "QQ分享"));
this.mShareItems.put(SOURCE_QZONE_BG, addShareItemText(a.a(SOURCE_QZONE_BG, getContext(), this.ninePatchRect), "空间分享"));
this.mContentView.addView(this.mRootView, layoutParams);
setContentView(this.mContentView);
}
private void calculateSize() {
Window window = getDialog().getWindow();
Point size = new Point();
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
if (mW < 0) {
mWidth = WindowManager.LayoutParams.WRAP_CONTENT;
} else if (mW > 1) {
mWidth = WindowManager.LayoutParams.MATCH_PARENT;
} else {
mWidth = (int) (size.x * mW);
}
if (mH < 0) {
mHeight = WindowManager.LayoutParams.WRAP_CONTENT;
} else if (mH > 1) {
mHeight = WindowManager.LayoutParams.MATCH_PARENT;
} else {
mHeight = (int) (size.y * mH);
}
}
/**
* 当对话框启动的时候被调用
*/
@Override
protected void onStart() {
super.onStart();
//设置弹窗的位置时在底部
Window window = getWindow();
// 设置显示动画
if (window != null) {
window.setWindowAnimations(R.style.BottomDialogAnimationStyle);
WindowManager.LayoutParams wl = window.getAttributes();
wl.x = 0;
wl.y = mActivity.getWindowManager().getDefaultDisplay().getHeight();
// 以下这两句是为了保证按钮可以水平满屏
wl.width = LayoutParams.MATCH_PARENT;
wl.height = LayoutParams.WRAP_CONTENT;
// 设置显示位置
onWindowAttributesChanged(wl);
}
}
/**
* {@inheritDoc}
*/
@Override
protected View createView(Context c) {
et = new EditText(c);
et.setBackgroundResource(R.drawable.oval);
et.setTextColor(c.getResources()
.getColorStateList(R.color.primary_text_holo_light));
et.setText(answer);
et.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if (!NumericType.NONE.equals(numericType)) {
KeyListener listener = getKeyListenerForType(numericType);
if (listener != null)
et.setKeyListener(listener);
} else {
et.setRawInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
return encapsulateQuestion(c, et);
}
@Override
protected void onPreExecute() {
isFinalizing = true;
recordFinish = true;
runAudioThread = false;
//创建处理进度条
creatingProgress= new Dialog(FFmpegRecorderActivity.this,R.style.Dialog_loading_noDim);
Window dialogWindow = creatingProgress.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = (int) (getResources().getDisplayMetrics().density*240);
lp.height = (int) (getResources().getDisplayMetrics().density*80);
lp.gravity = Gravity.CENTER;
dialogWindow.setAttributes(lp);
creatingProgress.setCanceledOnTouchOutside(false);
creatingProgress.setContentView(R.layout.activity_recorder_progress);
progress = (TextView) creatingProgress.findViewById(R.id.recorder_progress_progresstext);
bar = (ProgressBar) creatingProgress.findViewById(R.id.recorder_progress_progressbar);
creatingProgress.show();
//txtTimer.setVisibility(View.INVISIBLE);
//handler.removeCallbacks(mUpdateTimeTask);
super.onPreExecute();
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(context);
ll.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
ll.setMinimumHeight(50);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setGravity(Gravity.CENTER_VERTICAL);
ImageView iv = new ImageView(context);
iv.setImageResource(attributes.get(position).getIconResId());
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
if (listView.getCheckedItemPosition() == position) {
iv.setBackgroundResource(R.drawable.icon_hilight);
}
TextView tv = new TextView(context);
tv.setText(attributes.get(position).getDescriptionShort());
tv.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setPadding(10, 0, 0, 0);
tv.setTextSize(14.0f);
tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
tv.setTextColor(0xFFFFFFFF);
tv.setMinHeight(46);
ll.addView(iv);
ll.addView(tv);
return ll;
}
protected void setContentLayout(int viewId) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(viewId, null);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
contentView.setLayoutParams(layoutParams);
if (null != rootContent) {
rootContent.addView(contentView);
}
}
private void init() {
View view = LayoutInflater.from(mContext).inflate(
R.layout.zg_widget_pop_media_catalog, null);
ListView listView = (ListView) view.findViewById(R.id.listview);
mPopupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
mAdapter = new MediaCatalogAdapter(mContext, mMediaType);
mAdapter.setFiles(mCatalogs);
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(this);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
}
@Override
public View createWrapper(View root) {
ShadowOverlayContainer wrapper = new ShadowOverlayContainer(root.getContext());
wrapper.setLayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
wrapper.initialize(needsDefaultShadow(),
needsDefaultListSelectEffect(),
areChildRoundedCornersEnabled());
return wrapper;
}
public void setActivity(Activity activity) {
super.setActivity(activity);
if (isDialogMode()) {
// activity.setTheme(android.R.style.Theme_Dialog);
// activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
// if (Build.VERSION.SDK_INT >= 11) {
// try {
// ReflectHelper.invokeInstanceMethod(activity, "setFinishOnTouchOutside", false);
// } catch (Throwable e) {}
// }
}
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
/**
* Calculates control width and creates text layouts
*
* @param widthSize
* the input layout width
* @param mode
* the layout mode
* @return the calculated control width
*/
private int calculateLayoutWidth(int widthSize, int mode) {
initResourcesIfNecessary();
// TODO: make it static
itemsLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
itemsLayout
.measure(MeasureSpec.makeMeasureSpec(widthSize,
MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(
0, MeasureSpec.UNSPECIFIED));
int width = itemsLayout.getMeasuredWidth();
if (mode == MeasureSpec.EXACTLY) {
width = widthSize;
} else {
width += 2 * PADDING;
// Check against our minimum width
width = Math.max(width, getSuggestedMinimumWidth());
if (mode == MeasureSpec.AT_MOST && widthSize < width) {
width = widthSize;
}
}
itemsLayout.measure(MeasureSpec.makeMeasureSpec(width - 2 * PADDING,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
return width;
}
@Override
protected View onCreateDialogView() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6,6,6,6);
TextView promptTextView = new TextView(getContext());
promptTextView.setText(R.string.pressKey);
promptTextView.setGravity(Gravity.CENTER_HORIZONTAL);
valueTextView = new TextView(getContext());
valueTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
valueTextView.setGravity(Gravity.CENTER_HORIZONTAL);
valueTextView.setPadding(0, 12, 0, 12);
TextView alertTextView = new TextView(getContext());
alertTextView.setText(R.string.pressKeyInfo);
alertTextView.setGravity(Gravity.CENTER_HORIZONTAL);
alertTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
layout.addView(promptTextView, params);
layout.addView(valueTextView, params);
layout.addView(alertTextView);
return layout;
}
@Override
/**
* Ask the host application for a custom progress view to show while
* a <video> is loading.
* @return View The progress view.
*/
public View getVideoLoadingProgressView() {
if (mVideoProgressView == null) {
// Create a new Loading view programmatically.
// create the linear layout
LinearLayout layout = new LinearLayout(parentEngine.getView().getContext());
layout.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.setLayoutParams(layoutParams);
// the proress bar
ProgressBar bar = new ProgressBar(parentEngine.getView().getContext());
LinearLayout.LayoutParams barLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
barLayoutParams.gravity = Gravity.CENTER;
bar.setLayoutParams(barLayoutParams);
layout.addView(bar);
mVideoProgressView = layout;
}
return mVideoProgressView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setBackgroundResource(image[position % 13]);
imageView.setLayoutParams(new Gallery.LayoutParams(400, 300));
imageView.setScaleType(ScaleType.FIT_XY);
return imageView;
}
private void setupVrModeWindowFlags() {
if (mRestoreSystemUiVisibilityFlag == -1) {
mRestoreSystemUiVisibilityFlag = mActivity.getWindow().getDecorView()
.getSystemUiVisibility();
}
mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mActivity.getWindow().getDecorView().setSystemUiVisibility(VR_SYSTEM_UI_FLAGS);
}
private void displayViews(ViewGroup mainView) {
if(null != viewGroup){
viewGroup.setVisibility(View.VISIBLE);
viewGroup.addView(mainView);
}
else{
LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
activity.addContentView(mainView, params);
}
Animation fadeInAnimation = AnimationUtils.loadAnimation(activity, android.R.anim.fade_in);
mainView.startAnimation(fadeInAnimation);
if (onShowListener != null) onShowListener.onRateAppShowing();
}