android一些ui效果实现
TabLayout官方控件如何修改选中时和默认情况下字体大小、颜色?
TabLayout官方控件如何修改选中时和默认情况下字体大小、颜色?
1.xml中添加tablayout
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginTop="35dp"
app:tabTextColor="@color/color_EC"
app:tabIndicatorColor="@color/color_EC"
android:layout_centerHorizontal="true"
app:tabIndicatorFullWidth="false"
/>
这样添加后tablayout是默认的字体大小,在xml中无法修改该控件的字体大小,用了那个通过style文件修改的方法发现无论我怎么调试都无法实现效果,改用java代码中改变字体大小。
2.java代码中动态改变字体大小:
private TabLayout tabLayout;
tabLayout = rootView.findViewById(R.id.tab_layout);
private void initTabLayout() {
tabLayout.addTab(tabLayout.newTab().setText("豆浆"));
tabLayout.addTab(tabLayout.newTab().setText("粥类"));
tabLayout.addTab(tabLayout.newTab().setText("米糊"));
ColorStateList colorStateList = tabLayout.getTabTextColors();
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
assert tab != null;
String tabStr = Objects.requireNonNull(tab.getText()).toString();
if (tab.getCustomView() == null || !(tab.getCustomView() instanceof TextView)) {
TextView tv = new TextView(tabLayout.getContext());
tv.setTextColor(colorStateList);
tv.setText(tabStr);
tv.setTextSize(tab.isSelected() ? 26 : 22);
tab.setCustomView(tv);
}
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
TextView tv = (TextView) tab.getCustomView();
assert tv != null;
tv.setTextSize(26);
String mType = null;
switch (tab.getPosition()) {
case 0:
if (tab.getText().toString().equals("豆浆")) {
mType = "50d1f708594c47d0aa75c1ae6610eb49";
}
refreshData(mType);
refreshAdapter.refreshData();
break;
case 1:
if (tab.getText().toString().equals("粥类")) {
mType = "d78e226d2f874a6d974079a27f7b8524";
}
refreshData(mType);
refreshAdapter.refreshData();
break;
case 2:
if (tab.getText().toString().equals("米糊")) {
mType = "24be21a7fbbb4790a245ad4ac3825e60";
}
refreshData(mType);
refreshAdapter.refreshData();
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
TextView tv = (TextView) tab.getCustomView();
assert tv != null;
tv.setTextSize(22);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
java代码中声明xml中tablayout控件后,调用上面的initTabLayout函数即可;
动态改变文本内容、文本颜色、文本大小主要靠上面的for循环重新给每一个tab对象设置进去新创建的TextView
接下来监听未选中状态和选中状态下tab对象文本大小:
上述代码添加后tabLayout的使用效果:
上面的图片看出已经把tablayout默认的字体大小修改了,也监听了选中状态下和非选中状态下字体大小的改变。
toast字体居中?
Toast toast = Toast.makeText(context, text, duration);
centerText(toast.getView());
toast.show();
private void centerText(View view) {
if( view instanceof TextView){
((TextView) view).setGravity(Gravity.CENTER);
}else if( view instanceof ViewGroup){
ViewGroup group = (ViewGroup) view;
int n = group.getChildCount();
for( int i = 0; i<n; i++ ){
centerText(group.getChildAt(i));
}
}
}
Glide修改圆角
首先圆角值让ui告诉你或者使用Sketch软件查看圆角值;
接着使用Glide的load方法加载url链接;使用transform方法改变图片圆角值,使用into来加载到对应控件中;
private void setBitMap(@NotNull ViewHolder holder, int position) {
Glide.with(mContext)
.asBitmap()
.load(mStringArray[position])
.transform(new CenterCrop(), new RoundedCorners((30)))
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(((NormalHolder) holder).mImg);
}
还可以通过cardview来设置圆角值,注意要设置成new CenterCrop(),原因不知道。
将mipmap资源下的图片转换为url形式
/**
* 将mipmap资源下的图片转换为url形式
*
* @param id
* @return
*/
private String setDrawable(int id) {
Resources r = getResources();
Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ r.getResourcePackageName(id) + "/"
+ r.getResourceTypeName(id) + "/"
+ r.getResourceEntryName(id));
return uri.toString();
}
版权声明:本文为shengjiaxin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。