Android细节使用汇总

一、组件设置setCompoundDrawables不生效解决办法

在代码中设置组件的drawable的时候如果单纯的使用setCompoundDrawables是不会有效果的,因为没有指定drawable的大小,即Bound,具体代码如下:

Drawable drawable = getResources().getDrawable(R.drawable.ic_et_bg);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
mEt.setCompoundDrawables(drawable,null,null,null,null);

这种方式是可以指定drawable的大小的,还有另外一种方式,使用drawable的默认大小,如下:

Drawable drawable = getResources().getDrawable(R.drawable.ic_et_bg);
mEt.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null,null);

二、关于LinearLayout的对齐

如果设置LinearLayout为android:orientation=”horizontal”,那设置子控件的android:layout_gravity = “left”或android:layout_gravity = “right”是没有效果的。

三、动态改变RecyclerView的高度

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()) {
          @Override
          public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
             setMeasuredDimension(View.MeasureSpec.getSize(widthSpec),45*4+10*3);
            }

});

四、RecyclerView 自动滑动到底部

mRecyclerView.scrollToPosition(adapter.getItemCount()-1);

五、Button的英文字符都变大写

1.在布局文件Button下增加android:textAllCaps=”false”

2.在代码中,mButton.setTransformationMethod(null)

3.既然是主题引起的,我们可以自定义Button主题

<style name="MyButton" parent="Widget.AppCompat.Button">
     <item name="android:textAllCaps">false</item>
   </style>

4.自定义Button

public class MyButton extends Button { 
 public Button(Context context, AttributeSet attrs) {
     super(context, attrs);
     setSupportAllCaps(false); 
 }  
 public Button(Context context, AttributeSet attrs, int defStyleAttr) {
     super(context, attrs, defStyleAttr);
     setSupportAllCaps(false); 
 } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值