Android 开发一些小技巧

1.动态获取View 的id

String imgName = "scenic_image" + (i + 1);

int imgId = res.getIdentifier(imgName, "id", mActivity.getPackageName());

ImageView img = (ImageView) mView.findViewById(imgId);

2.动态修改背景

GradientDrawable background = (GradientDrawable) holder.tv_tag_text.getBackground();

background.setColor(Color.parseColor(entity.getTag_color()));


操作背景shape

针对设置了background属性的view,注意必须是shape的背景。可以通过getBackground获取背景并转化为GradientDrawable,进而设置背景边框颜色。当然也可以在GradientDrawable能力范围内设置其它属性。

GradientDrawable drawable = (GradientDrawable) view.getBackground().mutate();drawable.setStroke(1, color);

但是注意上面使用了getBackground().mutate(),原因可参考Drawable的mutate方法——多ImageView.setAlpha失效问题解决。

动态创建

即用代码动态创建一个GradientDrawable后,设置为view的背景。

GradientDrawable drawable=newGradientDrawable();drawable.setCornerRadius(10);drawable.setStroke(2,Color.BLUE);tv.setBackgroundDrawable(drawable);

作者:zizi192

链接:https://www.jianshu.com/p/ed9431d3c8f3

来源:

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


3.将文本内容放到系统剪贴板里

public voidcopyToClip(String link) {

// 从API11开始android推荐使用android.content.ClipboardManager

ClipboardManager cm = (ClipboardManager)mContext.getSystemService(Context.CLIPBOARD_SERVICE);

// 将文本内容放到系统剪贴板里。

cm.setPrimaryClip(ClipData.newPlainText(null,link));


设置屏幕亮度

WindowManager.LayoutParams params = getWindow().getAttributes();

params.flags|= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

params.screenBrightness=1;

getWindow().setAttributes(params);


4.webView加载富文本 和设置样式

webView.loadDataWithBaseURL(null, entity.getProductDetail(),"text/html","UTF-8",null);


//背景渐变

android:shape="rectangle">

android:angle="270"

android:centerColor="#4CAF50"

android:endColor="#2E7D32"

android:startColor="#81C784"

android:type="linear"/>


Toolbar设置返回

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

toolbar.setTitle("底部Tab");

setSupportActionBar(toolbar);

getSupportActionBar().setHomeButtonEnabled(true);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

toolbar.setNavigationOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View view) {

onBackPressed();

}

});



你可能感兴趣的:(Android 开发一些小技巧)