下面列出了android.widget.TextView#getLayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 设置当前SweetToast实例的最小宽高
* 很有用的功能,参考了简书上的文章:http://www.jianshu.com/p/491b17281c0a
* @param width SweetToast实例的最小宽度,单位是pix
* @param height SweetToast实例的最小高度,单位是pix
* @return
*/
public SweetToast minSize(int width, int height){
if(mContentView!=null && mContentView instanceof LinearLayout){
mContentView.setMinimumWidth(width);
mContentView.setMinimumHeight(height);
((LinearLayout)mContentView).setGravity(Gravity.CENTER);
try {
TextView textView = ((TextView) mContentView.findViewById(R.id.message));
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
params.height = LinearLayout.LayoutParams.MATCH_PARENT;
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);
}catch (Exception e){
Log.e("幻海流心","e:"+e.getLocalizedMessage());
}
}
return this;
}
/**
* 改变关注状态
*
* @param isAttention 是否关注
* @param button 关注按钮
* @param add +图片
* @param layout 父布局
*/
private void changeBtnAttention(boolean isAttention, TextView button, ImageView add, RelativeLayout layout) {
if (!isAttention) {
layout.setVisibility(View.VISIBLE);
layout.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.shape_blue_rect));
button.setText(R.string.personal_focus_someone);
add.setVisibility(View.VISIBLE);
button.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
} else {
layout.setVisibility(View.VISIBLE);
layout.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.shape_dark_gray_fill_4dp));
button.setText(R.string.personal_focused);
button.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
add.setVisibility(View.GONE);
}
}
/**
* 设置TextView(@+id/snackbar_text)左右两侧的图片
* @param leftDrawable
* @param rightDrawable
* @return
*/
public SnackbarUtils leftAndRightDrawable(@Nullable Drawable leftDrawable, @Nullable Drawable rightDrawable){
if(getSnackbar()!=null){
TextView message = (TextView) getSnackbar().getView().findViewById(R.id.snackbar_text);
LinearLayout.LayoutParams paramsMessage = (LinearLayout.LayoutParams) message.getLayoutParams();
paramsMessage = new LinearLayout.LayoutParams(paramsMessage.width, paramsMessage.height,0.0f);
message.setLayoutParams(paramsMessage);
message.setCompoundDrawablePadding(message.getPaddingLeft());
int textSize = (int) message.getTextSize();
Log.e("Jet","textSize:"+textSize);
if(leftDrawable!=null){
leftDrawable.setBounds(0,0,textSize,textSize);
}
if(rightDrawable!=null){
rightDrawable.setBounds(0,0,textSize,textSize);
}
message.setCompoundDrawables(leftDrawable,null,rightDrawable,null);
LinearLayout.LayoutParams paramsSpace = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
((SweetSnackbar.SnackbarLayout)getSnackbar().getView()).addView(new Space(getSnackbar().getView().getContext()),1,paramsSpace);
}
return this;
}
private void centerButton(TextView button) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
params.gravity = Gravity.CENTER_HORIZONTAL;
params.weight = 0.5f;
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.common_button);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.main_menu_item, null);
}
//绑定item内的TextView和RadioButton
TextView nameText = MenuDialogAdapterViewHolder.get(convertView, R.id.menu_item_textview);
RadioButton clickButton = MenuDialogAdapterViewHolder.get(convertView, R.id.radioButton);
clickButton.setChecked(selectedPos == position);//改变点击选中状态
//修改item高度,使其达到甲方要求的每页10个item显示要求
ViewGroup.LayoutParams lp = nameText.getLayoutParams();
lp.height = parent.getHeight() / 10;
//获取选中的item的标题
CommodityTypeModel menuData = menuDatas.get(position);
String str = menuData.getName();
nameText.setText(str);//设置标题
convertView.setSelected(selectedPos == position);//设置选中时的view
nameText.setSelected(selectedPos == position);//判断菜单的点击状态
//选中后的标题字体及RadioButton颜色
nameText.setTextColor(selectedPos == position ? 0xFF387ef5 : 0xFF222222);
clickButton.setTextColor(selectedPos == position ? 0xFF787878 : 0xFF387ef5);
return convertView;
}
@SuppressWarnings("deprecation")
private void updateShareTextViewParams(TextView textView) {
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.width = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);
textView.setBackgroundDrawable(null);
}
/**
* 创建取消按钮
*/
private void createCancel() {
if (TextUtils.isEmpty(mCancelStr)) {
return;
}
mTvCancel = new TextView(mContext);
mTvCancel.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mTvCancel.setMinimumHeight(mItemsMinHeight);
mTvCancel.setId(R.id.tv_cancelActionSheetDialog);
mLLayoutRoot.addView(mTvCancel);
mTvCancel.setLineSpacing(mLineSpacingExtra, mLineSpacingMultiplier);
mTvCancel.setGravity(mCancelGravity);
mTvCancel.setCompoundDrawablePadding(mTextDrawablePadding);
mTvCancel.setPadding(mItemsTextPaddingLeft, mItemsTextPaddingTop, mItemsTextPaddingRight, mItemsTextPaddingBottom);
mTvCancel.setText(mCancelStr);
mTvCancel.setTextSize(mTextSizeUnit, mCancelTextSize);
mTvCancel.setTextColor(mCancelTextColor);
if (mCancelMarginTop > 0) {
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mTvCancel.getLayoutParams();
if (lp != null) {
lp.topMargin = mCancelMarginTop;
mTvCancel.setLayoutParams(lp);
}
}
boolean single = mCancelMarginTop > 0 || TextUtils.isEmpty(mTitleStr) && mViewItem == null || mBottomDrawable == null;
setViewBackground(mTvCancel, DrawableUtil.getNewDrawable(single ? mStateDrawableSingle : mStateDrawableBottom));
mTvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
setTextViewLine(mTvCancel);
}
private void configureTextView(final TextView text, final float textSize, final int textColor,
final int textMaxLines,
final int gravity) {
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
text.setTextColor(textColor);
if (textMaxLines != DEFAULT_MAX_LINES) {
text.setMaxLines(textMaxLines);
}
final LayoutParams titleParams = (LayoutParams) text.getLayoutParams();
titleParams.gravity = gravity;
text.setLayoutParams(titleParams);
}
@Test
public void shouldHaveDefaultMargin() {
RobolectricActivity activity = rule.getActivity();
TextView textView = (TextView) activity.findViewById(R.id.hello_textview);
int bottomMargin = ((LinearLayout.LayoutParams) textView.getLayoutParams()).bottomMargin;
assertEquals(dpToPx(activity.getApplicationContext(),5), bottomMargin);
int topMargin = ((LinearLayout.LayoutParams) textView.getLayoutParams()).topMargin;
assertEquals(dpToPx(activity.getApplicationContext(),5), topMargin);
int rightMargin = ((LinearLayout.LayoutParams) textView.getLayoutParams()).rightMargin;
assertEquals(dpToPx(activity.getApplicationContext(),10), rightMargin);
int leftMargin = ((LinearLayout.LayoutParams) textView.getLayoutParams()).leftMargin;
assertEquals(dpToPx(activity.getApplicationContext(),10) , leftMargin);
}
/**
* 设置清晰度等级
* @param items 清晰度等级items
* @param defaultChecked 默认选中的清晰度索引
*/
public void setClarityGrade(List<String> items, int defaultChecked) {
mCurrentCheckedIndex = defaultChecked;
for (int i = 0; i < items.size(); i++) {
TextView itemView = (TextView) LayoutInflater.from(getContext())
.inflate(R.layout.change_video_clarity, mLinearLayout, false);
itemView.setTag(i);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
int checkIndex = (int) v.getTag();
if (checkIndex != mCurrentCheckedIndex) {
for (int j = 0; j < mLinearLayout.getChildCount(); j++) {
mLinearLayout.getChildAt(j).setSelected(checkIndex == j);
}
mListener.onClarityChanged(checkIndex);
mCurrentCheckedIndex = checkIndex;
} else {
mListener.onClarityNotChanged();
}
}
ChangeClarityDialog.this.dismiss();
}
});
itemView.setText(items.get(i));
itemView.setSelected(i == defaultChecked);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
itemView.getLayoutParams();
params.topMargin = (i == 0) ? 0 : VideoPlayerUtils.dp2px(getContext(), 16f);
mLinearLayout.addView(itemView, params);
}
}
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null) {
clear();
TextView tvStats = (TextView) findViewById(R.id.tvStats);
TextView tvState = (TextView) findViewById(R.id.tvState);
ProgressBar pbFilter = (ProgressBar) findViewById(R.id.pbFilter);
pbFilter.setVisibility(ProgressBar.GONE);
tvStats.setVisibility(TextView.VISIBLE);
runOnUiThread(new Runnable() {
@Override
public void run() {
setProgress(getString(R.string.title_restrict), 0, 1);
}
});
// Adjust progress state width
RelativeLayout.LayoutParams tvStateLayout = (RelativeLayout.LayoutParams) tvState.getLayoutParams();
tvStateLayout.addRule(RelativeLayout.LEFT_OF, R.id.tvStats);
if (results.values == null)
notifyDataSetInvalidated();
else {
addAll((ArrayList<ApplicationInfoEx>) results.values);
notifyDataSetChanged();
}
AppListAdapter.this.showStats();
}
}
/**
* 初始化
*/
private void init() {
View.inflate(mContext, R.layout.expand_text_view, this);
mTitleView = (TextView) findViewById(R.id.tv_title);
mContentView = (TextView) findViewById(R.id.tv_content);
mHintView = (TextView) findViewById(R.id.tv_more_hint);
mIndicateImage = (ImageView) findViewById(R.id.iv_arrow_more);
mShowMore = (RelativeLayout) findViewById(R.id.rl_show_more);
mTitleView.setText(title);
mTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
mTitleView.setTextColor(titleTextColor);
mContentView.setText(content);
mContentView.setTextSize(TypedValue.COMPLEX_UNIT_PX, contentTextSize);
mContentView.setTextColor(contentTextColor);
mHintView.setText(expandHint);
mHintView.setTextSize(TypedValue.COMPLEX_UNIT_PX, hintTextSize);
mHintView.setTextColor(hintTextColor);
if (indicateImage == null) {
indicateImage = getResources().getDrawable(R.drawable.ic_arrow_down_light_round);
}
mIndicateImage.setImageDrawable(indicateImage);
mShowMore.setOnClickListener(this);
ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams();
layoutParams.height = getMinMeasureHeight();
mContentView.setLayoutParams(layoutParams);
}
private void setCorrectFrameFigure(ImageView correctFrameFigure, int index) {
TextView correctFrameTextView = (TextView) findViewById(R.id.correctFigure_1);
int correctFrameFigureWidth = correctFrameTextView.getLayoutParams().width;
int correctFrameFigureHeight = correctFrameTextView.getLayoutParams().height;
if (level.GetTimeAttackCorrectFigures(index).equals("k")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.kwadrat_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
} else if (level.GetTimeAttackCorrectFigures(index).equals("o")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.okrag_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
} else if (level.GetTimeAttackCorrectFigures(index).equals("r")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.romb_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
} else if (level.GetTimeAttackCorrectFigures(index).equals("s")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.skat_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
} else if (level.GetTimeAttackCorrectFigures(index).equals("rf")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.romb_f_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
} else if (level.GetTimeAttackCorrectFigures(index).equals("oz")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.okrag_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
} else if (level.GetTimeAttackCorrectFigures(index).equals("ok")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.okat_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
} else if (level.GetTimeAttackCorrectFigures(index).equals("q")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.question_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
}else if (level.GetTimeAttackCorrectFigures(index).equals("kf")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.kwadrat_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
}else if (level.GetTimeAttackCorrectFigures(index).equals("kb")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.kwadrat_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
}else if (level.GetTimeAttackCorrectFigures(index).equals("rg")) {
correctFrameFigure.setImageBitmap(BitmapManager.decodeSampledBitmapFromResource(getResources(),
R.drawable.romb_ramka, correctFrameFigureWidth, correctFrameFigureHeight));
}
}
private View createTabIndicator(String label, Drawable drawable) {
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
TextView txtTitle = (TextView) tabIndicator.findViewById(R.id.text_view_tab_title);
txtTitle.setText(label);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) txtTitle.getLayoutParams();
txtTitle.setLayoutParams(params);
ImageView imgIcon = (ImageView) tabIndicator.findViewById(R.id.image_view_tab_icon);
imgIcon.setImageDrawable(drawable);
return tabIndicator;
}
/**
* @param gravity gravity of badge (TOP|LEFT ..etc)
* @return this, to allow builder pattern
*/
public T setGravity(int gravity) {
this.mGravity = gravity;
if (isWeakReferenceValid()) {
TextView textView = mTextViewRef.get();
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) textView.getLayoutParams();
layoutParams.gravity = gravity;
textView.setLayoutParams(layoutParams);
}
return getSubInstance();
}
private static void AdjustHorizotalRightAlignmentOutOfBounds(TextView tipView, ViewGroup root,
Point point, RxCoordinates anchorViewRxCoordinates,
RxCoordinates rootLocation) {
ViewGroup.LayoutParams params = tipView.getLayoutParams();
int rootLeft = rootLocation.left + root.getPaddingLeft();
if (point.x < rootLeft){
int availableSpace = anchorViewRxCoordinates.right - rootLeft;
point.x = rootLeft;
params.width = availableSpace;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
tipView.setLayoutParams(params);
measureViewWithFixedWidth(tipView, params.width);
}
}
@Override
protected void initView(View view, Bundle args) {
super.initView(view, args);
// 创建对象
// mRimetPresenter = new RimetPresenter(getPluginManager(), this);
setTitle(Constant.Name.TITLE);
TextView tvExt = sivSettingsLocation.getExtendView();
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) tvExt.getLayoutParams();
params.leftMargin = DisplayUtil.dip2px(getContext(), 100);
tvExt.setMaxLines(2);
tvExt.setEllipsize(TextUtils.TruncateAt.END);
tvExt.setTextSize(12);
SharedPreferences preferences = getDefaultSharedPreferences();
XPlugin xPlugin = getPluginManager().getXPluginById(Constant.Plugin.DING_DING);
sivLuckyEnable.bind(getDefaultSharedPreferences(),
Integer.toString(Constant.XFlag.ENABLE_LUCKY), true,
(view1, key, value) -> {
xPlugin.setEnable(Constant.XFlag.ENABLE_LUCKY, value);
return true;
});
sivLuckyDelayed.bind(getDefaultSharedPreferences(),
Integer.toString(Constant.XFlag.LUCKY_DELAYED), "",
(view12, key, value) -> true);
sivFastLuckyEnable.bind(getDefaultSharedPreferences(),
Integer.toString(Constant.XFlag.ENABLE_FAST_LUCKY), true,
(view1, key, value) -> {
xPlugin.setEnable(Constant.XFlag.ENABLE_FAST_LUCKY, value);
return true;
});
sivRecallEnable.bind(getDefaultSharedPreferences(),
Integer.toString(Constant.XFlag.ENABLE_RECALL), true,
(view1, key, value) -> {
xPlugin.setEnable(Constant.XFlag.ENABLE_RECALL, value);
return true;
});
sivLocationEnable.bind(getDefaultSharedPreferences(),
Integer.toString(Constant.XFlag.ENABLE_LOCATION), false,
(view1, key, value) -> {
xPlugin.setEnable(Constant.XFlag.ENABLE_LOCATION, value);
return true;
});
// 设置初始信息
sivSettingsLocation.setExtend(preferences.getString(
Integer.toString(Constant.XFlag.ADDRESS), ""));
sivSettingsLocation.setOnClickListener(v -> {
// 跳转到地图界面
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(BuildConfig.APPLICATION_ID, MapActivity.class.getName());
startActivityForResult(intent, 99);
});
sivLove.setOnClickListener(v -> {
// 打开捐赠界面
LoveDialog loveDialog = new LoveDialog();
loveDialog.show(getFragmentManager(), "love");
});
sivAbout.setOnClickListener(v -> {
// 打开关于界面
DialogUtil.showAboutDialog(getContext());
});
// 是否支持版本
XConfig xConfig = getPluginManager().getVersionManager().getSupportConfig();
setPromptText(xConfig != null ? "" : "不支持当前版本!");
}
public TextView addSubItem(int id, CharSequence text) {
createPopupLayout();
TextView textView = new TextView(getContext());
textView.setTextColor(Theme.getColor(Theme.key_actionBarDefaultSubmenuItem));
textView.setBackgroundDrawable(Theme.getSelectorDrawable(false));
if (!LocaleController.isRTL) {
textView.setGravity(Gravity.CENTER_VERTICAL);
} else {
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
}
textView.setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setMinWidth(AndroidUtilities.dp(196));
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setTag(id);
textView.setText(text);
popupLayout.addView(textView);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
if (LocaleController.isRTL) {
layoutParams.gravity = Gravity.RIGHT;
}
layoutParams.width = LayoutHelper.MATCH_PARENT;
layoutParams.height = AndroidUtilities.dp(48);
textView.setLayoutParams(layoutParams);
textView.setOnClickListener(view -> {
if (popupWindow != null && popupWindow.isShowing()) {
if (processedPopupClick) {
return;
}
processedPopupClick = true;
if (!allowCloseAnimation) {
popupWindow.setAnimationStyle(R.style.PopupAnimation);
}
popupWindow.dismiss(allowCloseAnimation);
}
if (parentMenu != null) {
parentMenu.onItemClick((Integer) view.getTag());
} else if (delegate != null) {
delegate.onItemClick((Integer) view.getTag());
}
});
return textView;
}
private void initNonCopyrightBtn(TextView textView) {
textView.setBackgroundResource(R.drawable.noncopyright_album_play_btn);
textView.getLayoutParams().width = UIsUtils.dipToPx(40.0f);
textView.getLayoutParams().height = UIsUtils.dipToPx(19.0f);
textView.setTextColor(Color.parseColor("#ccfcfcfc"));
}
protected RelativeLayout.LayoutParams getParamsLayoutOffset(TextView layout, T itemData) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.leftMargin = itemMargin * itemData.getTreeDepth() + offsetMargin;
return params;
}