java代码动态设置属性

1.java代码设置editText的最大长度

editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});

2.动态设置组件的宽高

LayoutParams layoutParams = id_drawer.getLayoutParams();
layoutParams.width = (int) getResources().getDimension(R.dimen.left_right_margin);

3.动态改变Background后Padding无效的问题

1.1方法一
    int bottom = theView.getPaddingBottom();
    int top = theView.getPaddingTop();
    int right = theView.getPaddingRight();
    int left = theView.getPaddingLeft();
    theView.setBackgroundResource(R.drawable.entry_bg_with_image);
    theView.setPadding(left, top, right, bottom);

1.2方法二 
    int pad = resources.getDimensionPixelSize(R.dimen.linear_layout_padding);
    theView.setBackgroundResource(R.drawable.entry_bg_with_image);
    theView.setPadding(pad, pad, pad, pad);

实际上就是在setBackgroundResource之后重新设置一下padding。 

4.动态设置窗体的背景色透明
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
5.java代码中使用字库

import android.graphics.Typeface;

mTypeface = Typeface.createFromAsset(getAssets(),"fonts/Roboto-Regular.ttf");
mTimerTextView.setTypeface(mTypeface);

其中,apk/assets/fonts/Roboto-Regular.ttf为特定的字体库

猜你喜欢

转载自blog.csdn.net/sindyue/article/details/74668927