下面列出了android.view.Gravity#CENTER 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initView(Context context, AttributeSet attrs) {
this.mContext = context;
if (null == attrs) {
if (!(getLayoutParams() instanceof FrameLayout.LayoutParams)) {
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(
dip2Px(50),
dip2Px(50),
Gravity.CENTER);
setLayoutParams(layoutParams);
}
}
mPaint1 = new Paint();
mPaint1.setAntiAlias(true);
mPaint1.setColor(Color.WHITE);
mPaint2 = new Paint();
mPaint2.setAntiAlias(true);
mPaint2.setStyle(Paint.Style.STROKE);
mPaint2.setColor(Color.WHITE);
}
@Override
protected WindowManager.LayoutParams createLayoutParams(Context ctx) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.packageName = ctx.getPackageName();
layoutParams.alpha = 1;
layoutParams.type = getOverlayType();
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
layoutParams.gravity = Gravity.CENTER;
layoutParams.format = PixelFormat.TRANSLUCENT;
layoutParams.x = 0;
layoutParams.y = 0;
layoutParams.width = 0;
layoutParams.height = 0;
return layoutParams;
}
public static String getHorizontalAlignment(android.view.View view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params instanceof FrameLayout.LayoutParams) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params;
if (Gravity.isHorizontal(lp.gravity)) {
switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT:
return "left";
case Gravity.CENTER:
return "center";
case Gravity.RIGHT:
return "right";
case Gravity.FILL_HORIZONTAL:
return "stretch";
}
}
}
return "stretch";
}
public Background(String bg) {
int g = Gravity.CENTER;
float scale = 1f;
String url = null;
for (String part : bg.split("\\s+")) {
if (part.endsWith("%")) {
try {
scale = Float.parseFloat(part.substring(0, part.length() - 1)) / 100;
} catch (NumberFormatException ignored) {}
} else if (GRAVITY.containsKey(part)) {
g = GRAVITY.get(part);
} else if (part.contains("://")){
url = part;
}
}
this.url = url;
this.scale = scale;
this.gravity = g;
}
/**
* @return a Button which is centred in its parent and provides visual feedback when clicked
*/
private Button createButton() {
final Button button = new Button(getContext());
button.setText("Button inside fragment");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setText("Button click successful...");
}
});
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup
.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
button.setLayoutParams(layoutParams);
return button;
}
private void initChildView(Context context) {
progressWheel = new ProgressWheel(context);
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
progressWheel.setVisibility(GONE);
progressWheel.setAlpha(0);
progressWheel.setBarWidth((int) DeviceUtils.dpToPx(context, 3));
progressWheel.setBarColor(getResources().getColor(R.color.white));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
progressWheel.setTranslationZ(DeviceUtils.dpToPx(context, 4));
}
super.addView(progressWheel, -1, params);
}
private static FrameLayout.LayoutParams convertEmptyViewLayoutParams(ViewGroup.LayoutParams lp) {
FrameLayout.LayoutParams newLp = null;
if (null != lp) {
newLp = new FrameLayout.LayoutParams(lp);
if (lp instanceof LinearLayout.LayoutParams) {
newLp.gravity = ((LinearLayout.LayoutParams) lp).gravity;
} else {
newLp.gravity = Gravity.CENTER;
}
}
return newLp;
}
@Override
public BuildBean assignMdLoadingVertical(Context context, CharSequence msg, boolean cancleable, boolean outsideTouchable, boolean isWhiteBg) {
BuildBean bean = new BuildBean();
bean.mContext = context;
bean.msg = msg;
bean.isWhiteBg = isWhiteBg;
bean.gravity = Gravity.CENTER;
bean.cancelable = cancleable;
bean.outsideTouchable = outsideTouchable;
bean.type = CommonConfig.TYPE_MD_LOADING_VERTICAL;
return bean;
}
@SuppressLint("InlinedApi")
private void layoutDesign() {
setOrientation(LinearLayout.VERTICAL);
FrameLayout.LayoutParams lParams0 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
setLayoutParams(lParams0);
setBackgroundColor(Color.TRANSPARENT);
ProgressBar progressBar = new ProgressBar(mContext);
progressBar.setBackgroundColor(Color.TRANSPARENT);
LayoutParams lParams1 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lParams1.gravity = Gravity.CENTER;
progressBar.setLayoutParams(lParams1);
addView(progressBar);
TextView textView = new TextView(mContext);
textView.setText(mMessage);
LayoutParams lParams2 = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
lParams2.gravity = Gravity.CENTER;
lParams2.width = mScreenWidth;
textView.setGravity(Gravity.CENTER);
textView.setLayoutParams(lParams2);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setPadding(mMessagePadding, mMessagePadding, mMessagePadding, mMessagePadding);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize);
textView.setTextColor(0xffe5e5e5);
addView(textView);
}
private void createProgressView() {
mCircleView = new CircleImageView(getContext(), CIRCLE_BG_LIGHT, CIRCLE_DIAMETER / 2);
mProgress = new MaterialProgressDrawable(getContext(), this);
mProgress.setBackgroundColor(CIRCLE_BG_LIGHT);
mCircleView.setImageDrawable(mProgress);
mCircleView.setVisibility(View.GONE);
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
mCircleView.setLayoutParams(params);
addView(mCircleView);
}
private void setTitles(Context context, String[] titles, final boolean smoothScroll)
{
this.textViews = new TextView[titles.length];
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT);
params.weight = 1;
params.gravity = Gravity.CENTER;
// 循环,根据标题栏动态生成TextView来显示标题,每个标题栏的宽度比例为1:1,其中的内容居中。
for(int i = 0; i < titles.length; i++)
{
final int index = i;
TextView textView = new TextView(context);
textView.setText(titles[i]);
textView.setGravity(Gravity.CENTER);
textViews[i] = textView;
textViews[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(index, smoothScroll);
if(onTitleClickListener != null)
{
onTitleClickListener.onTitleClick(v);
}
}
});
titleLayout.addView(textView, params);
}
}
public RxDialogAcfunVideoLoading(Context context) {
super(context);
// TODO Auto-generated constructor stub
View dialogView = LayoutInflater.from(context).inflate(
R.layout.dialog_loading_progress_acfun_video, null);
loading_progressBar = (ProgressBar) dialogView.findViewById(R.id.loading_progressBar);
Random random = new Random();
int number = Math.abs(random.nextInt() % loadingText.length);
tv_reminder = (TextView) dialogView.findViewById(R.id.tv_reminder);
tv_reminder.setText(loadingText[number]);
setContentView(dialogView);
getLayoutParams().gravity = Gravity.CENTER;
}
private static FrameLayout.LayoutParams convertEmptyViewLayoutParams(ViewGroup.LayoutParams lp) {
FrameLayout.LayoutParams newLp = null;
if (null != lp) {
newLp = new FrameLayout.LayoutParams(lp);
if (lp instanceof LinearLayout.LayoutParams) {
newLp.gravity = ((LinearLayout.LayoutParams) lp).gravity;
} else {
newLp.gravity = Gravity.CENTER;
}
}
return newLp;
}
private void initViews() {
mImgWheel = new ImageView(getContext());
if (mWheelDrawable != null) {
mImgWheel.setImageDrawable(mWheelDrawable);
}
FrameLayout.LayoutParams wheelParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
wheelParams.leftMargin= (int) dpToPx(getContext(),16);
wheelParams.topMargin= (int) dpToPx(getContext(),16);
wheelParams.rightMargin= (int) dpToPx(getContext(),16);
wheelParams.bottomMargin= (int) dpToPx(getContext(),16);
wheelParams.gravity = Gravity.CENTER;
addView(mImgWheel, wheelParams);
mImgThumb = new ImageView(getContext());
if (mThumbDrawable != null) {
mImgThumb.setImageDrawable(mThumbDrawable);
}
FrameLayout.LayoutParams thumbParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
thumbParams.gravity = Gravity.CENTER;
addView(mImgThumb, thumbParams);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mImgThumb.setStateListAnimator(AnimatorInflater.loadStateListAnimator(getContext(), R.anim.raise));
mImgThumb.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, mImgThumb.getMeasuredWidth(), mImgThumb.getMeasuredHeight());
}
});
}
}
public void showTable() {
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
layoutParams.leftMargin = 30;
layoutParams.bottomMargin = 10;
layoutParams.topMargin = 10;
for (int i = 0; i < 40; i++) {
TableRow tableRow = new TableRow(this);
TextView textView = new TextView(this);
textView.setText("Test stretch scroll view " + i);
textView.setTextSize(20);
textView.setPadding(15, 15, 15, 15);
tableRow.addView(textView, layoutParams);
if (i % 2 != 0) {
tableRow.setBackgroundColor(Color.LTGRAY);
} else {
tableRow.setBackgroundColor(Color.WHITE);
}
final int n = i;
tableRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(StretchViewActivity.this, "Click item " + n, Toast.LENGTH_SHORT).show();
}
});
mMainLayout.addView(tableRow);
}
}
/**
* 创建启动时的界面Layout。
*
* @return 返回创建的界面Layout.
*/
private View createLayout() {
FrameLayout layout = new FrameLayout(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);
layout.setBackgroundColor(getBackgroundColor());
mSplashImage = new ImageView(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
layout.addView(mSplashImage, params);
return layout;
}
private void applyLayoutParams() {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
switch (badgePosition) {
case POSITION_TOP_LEFT:
lp.gravity = Gravity.LEFT | Gravity.TOP;
lp.setMargins(badgeMarginH, badgeMarginV, 0, 0);
break;
case POSITION_TOP_RIGHT:
lp.gravity = Gravity.RIGHT | Gravity.TOP;
lp.setMargins(0, badgeMarginV, badgeMarginH, 0);
break;
case POSITION_BOTTOM_LEFT:
lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
lp.setMargins(badgeMarginH, 0, 0, badgeMarginV);
break;
case POSITION_BOTTOM_RIGHT:
lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
lp.setMargins(0, 0, badgeMarginH, badgeMarginV);
break;
case POSITION_CENTER:
lp.gravity = Gravity.CENTER;
lp.setMargins(0, 0, 0, 0);
break;
default:
break;
}
setLayoutParams(lp);
}
/**
* Programmatically build up the view hierarchy to avoid the need for resources.
*
* @return View hierarchy
*/
private View generateHierarchy(Context context) {
Resources resources = getResources();
LayoutParams params;
int fivePx = dpToPx(5, resources);
int tenPx = dpToPx(10, resources);
int twentyPx = dpToPx(20, resources);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(
0,
ViewGroup.LayoutParams.WRAP_CONTENT,
1f);
tableLayoutParams.setMargins(0, 0, fivePx, 0);
LinearLayout seekWrapper;
FrameLayout root = new FrameLayout(context);
params = createLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(300, resources));
root.setLayoutParams(params);
FrameLayout container = new FrameLayout(context);
params = createMatchParams();
params.setMargins(0, twentyPx, 0, 0);
container.setLayoutParams(params);
container.setBackgroundColor(Color.argb(100, 0, 0, 0));
root.addView(container);
mSpringSelectorSpinner = new Spinner(context, Spinner.MODE_DIALOG);
params = createMatchWrapParams();
params.gravity = Gravity.TOP;
params.setMargins(tenPx, tenPx, tenPx, 0);
mSpringSelectorSpinner.setLayoutParams(params);
container.addView(mSpringSelectorSpinner);
LinearLayout linearLayout = new LinearLayout(context);
params = createMatchWrapParams();
params.setMargins(0, 0, 0, dpToPx(80, resources));
params.gravity = Gravity.BOTTOM;
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(LinearLayout.VERTICAL);
container.addView(linearLayout);
seekWrapper = new LinearLayout(context);
params = createMatchWrapParams();
params.setMargins(tenPx, tenPx, tenPx, twentyPx);
seekWrapper.setPadding(tenPx, tenPx, tenPx, tenPx);
seekWrapper.setLayoutParams(params);
seekWrapper.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(seekWrapper);
mTensionSeekBar = new SeekBar(context);
mTensionSeekBar.setLayoutParams(tableLayoutParams);
seekWrapper.addView(mTensionSeekBar);
mTensionLabel = new TextView(getContext());
mTensionLabel.setTextColor(mTextColor);
params = createLayoutParams(
dpToPx(50, resources),
ViewGroup.LayoutParams.MATCH_PARENT);
mTensionLabel.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
mTensionLabel.setLayoutParams(params);
mTensionLabel.setMaxLines(1);
seekWrapper.addView(mTensionLabel);
seekWrapper = new LinearLayout(context);
params = createMatchWrapParams();
params.setMargins(tenPx, tenPx, tenPx, twentyPx);
seekWrapper.setPadding(tenPx, tenPx, tenPx, tenPx);
seekWrapper.setLayoutParams(params);
seekWrapper.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(seekWrapper);
mFrictionSeekBar = new SeekBar(context);
mFrictionSeekBar.setLayoutParams(tableLayoutParams);
seekWrapper.addView(mFrictionSeekBar);
mFrictionLabel = new TextView(getContext());
mFrictionLabel.setTextColor(mTextColor);
params = createLayoutParams(dpToPx(50, resources), ViewGroup.LayoutParams.MATCH_PARENT);
mFrictionLabel.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
mFrictionLabel.setLayoutParams(params);
mFrictionLabel.setMaxLines(1);
seekWrapper.addView(mFrictionLabel);
View nub = new View(context);
params = createLayoutParams(dpToPx(60, resources), dpToPx(40, resources));
params.gravity = Gravity.TOP | Gravity.CENTER;
nub.setLayoutParams(params);
nub.setOnTouchListener(new OnNubTouchListener());
nub.setBackgroundColor(Color.argb(255, 0, 164, 209));
root.addView(nub);
return root;
}
@Override
public LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
videoPlayer.setOnPreparedListener(this);
videoPlayer.setOnBufferingUpdateListener(this);
videoPlayer.setOnCompletionListener(this);
videoPlayer.setOnErrorListener(this);
videoPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
textureView = new TextureView(getContext());
addView(textureView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout rl = new LinearLayout(getContext());
addView(rl, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
rl.setId(R.id.touchId);
rl.setOnClickListener(this);
LayoutInflater li = LayoutInflater.from(getContext());
videoLoadingView = li.inflate(R.layout.layout_video_loading_view, this, false);
addView(videoLoadingView);
controlPlayPause = li.inflate(R.layout.layout_video_img_button_play_pause, this, false);
controlSeekBar = li.inflate(R.layout.layout_video_seek_bar, this, false);
FrameLayout.LayoutParams lp1 = (FrameLayout.LayoutParams) controlPlayPause.getLayoutParams();
FrameLayout.LayoutParams lp2 = (FrameLayout.LayoutParams) controlSeekBar.getLayoutParams();
lp1.gravity = Gravity.CENTER;
lp2.gravity = Gravity.BOTTOM;
addView(controlPlayPause, lp1);
addView(controlSeekBar, lp2);
imgBtnPlayPause = (ImageButton) controlPlayPause.findViewById(R.id.imageButtonPlayPauseRetry);
imgBtnFullScreenToggle = (ImageButton) controlSeekBar.findViewById(R.id.imageButtonFullScreenToggle);
tvPosition = (TextView) controlSeekBar.findViewById(R.id.textViewPosition);
tvDuration = (TextView) controlSeekBar.findViewById(R.id.textViewDuration);
proViewVideoLoading = (ProgressView) videoLoadingView.findViewById(R.id.proViewVideoLoading);
seekBarDuration = (SeekBar) controlSeekBar.findViewById(R.id.seekBarDuration);
imgBtnPlayPause.setImageDrawable(playVideoDrawable);
imgBtnPlayPause.setOnClickListener(this);
imgBtnFullScreenToggle.setOnClickListener(this);
textureView.setSurfaceTextureListener(this);
seekBarDuration.setOnSeekBarChangeListener(this);
controlPlayPause.setVisibility(INVISIBLE);
controlSeekBar.setVisibility(INVISIBLE);
proViewVideoLoading.start();
setUpVideoPlayer();
}