android开发日常问题总结

2016.09.20

 

 1. 如何用RGB设置背景色

public void recoverBottomBgColor(String color){

        String mcolor = "#"+color;

        mTabHost.getTabWidget().setBackgroundColor(Color.parseColor(mcolor));

    }

 

2.EventBus

 

3.把一个路径图片URL设置给一个控件

AndroidBitmapDrawable相互转换的方法

 

      

 

 

4.控件之间逻辑分层,便于维护,松耦合  

 

Header 包括bannerbanner中又有Indictor.

 

 

 

 

 

 

 

 Header

        

  Banner

 

  

           

 

5. 设置控件背景。

 

        

6.ImageView scaleType属性

 

7.Animation

8.EditText滚动和ScrollView滚动冲突

 EditText的自定义文件添加方法:FeedBackEditText.java

setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
            if (canScrollVertically(-1) || canScrollVertically(0)) {
            getParent().requestDisallowInterceptTouchEvent(true);
            if (event.getAction() == MotionEvent.ACTION_UP) {
             getParent().requestDisallowInterceptTouchEvent(false);
            }
        }
        return false;
    }
});

9.意见反馈,反复提交问题:由于网络延迟,造成意见重复提交。设置submit提交按钮提交时设置成Submit.isEnabled(false),然后再设置成true

 

10.

 

Warning:Exception while processing task

java.io.FileNotFoundException: C:\Users\lenovo\AppData\Local\Android\sdk\tools\proguard\proguard-android.txt (系统找不到指定的路径。)  :identity:transformClassesAndResourcesWithProguardForRelease FAILED

 

 

 

 

 

 

 

 

 

 

Error:Execution failed for task ':identity:transformClassesAndResourcesWithProguardForRelease'.

> java.io.FileNotFoundException: C:\Users\lenovo\AppData\Local\Android\sdk\tools\proguard\proguard-android.txt (系统找不到指定的路径。)

 

解决方案:用其他的tools文件夹替换掉原来的tools文件夹

 

11.如何使图片不拉伸

    android:scaleType="centerInside"

 

12.Android开发中,大部分控件都有visibility这个属性,其属性有3个分别为“visible ”、“invisible”、“gone”。主要用来设置控制控件的显示和隐藏

可见(visible)

XML文件:android:visibility="visible"

Java代码:view.setVisibility(View.VISIBLE);

 

不可见(invisible

XML文件:android:visibility="invisible"

Java代码:view.setVisibility(View.INVISIBLE);

 

隐藏(GONE

XML文件:android:visibility="gone"

Java代码:view.setVisibility(View.GONE);

例子:

<TextView
    android:id="@+id/tv_message_num"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/layer_title_search_fragment_et"
    android:layout_marginRight="36dp"
    android:background="@drawable/bg_home_message_50"
    android:gravity="center"
    android:textColor="@color/white"

    android:textSize="@dimen/cart_number_size"
    android:visibility="gone"
/>

 

Invisible 布局控件看不到,但是占有空间,Gone布局控件不显示,而且不占有空间。

13.ImageView的属性:

android:scaleType
android:scaleType是控制图片如何resized/moved来匹对ImageViewsizeImageView.ScaleType / android:scaleType值的意义区别:

CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示

CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长()等于或大于View的长()

CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/

FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示

FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示

MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。

** 要注意一点,Drawable文件夹里面的图片命名是不能大写的。

14.RelativeLayout的属性

 

第一类:属性值为truefalse
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离


EditTextandroid:hint

设置EditText为空时输入框内的提示信息。

android:gravity
android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text view的靠左,靠右等位置.以button为例,android:gravity="right"button上面的文字靠右

android:layout_gravity
android:layout_gravity是用来设置该view相对与起父view 的位置.比如一个button linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为例,android:layout_gravity="right"button靠右

android:layout_alignParentRight

使当前控件的右端和父控件的右端对齐。这里属性值只能为truefalse,默认false

 

14.

android setCompoundDrawablessetCompoundDrawablesWithIntrinsicBounds区别 

手工设置文本与图片相对位置时,常用到如下方法:

setCompoundDrawables(left, top, right, bottom)


setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)


意思是设置Drawable显示在text的左、上、右、下位置。


但是两者有些区别:
setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,
所以才有The Drawables must already have had setBounds(Rect) called.

使用之前必须使用Drawable.setBounds设置Drawable的长宽。


setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,
所以才有The Drawables' bounds will be set to their intrinsic bounds.

即通过getIntrinsicWidth()getIntrinsicHeight()获得,

 

xml中的textView:

<TextView

android:id="@+id/bookTitle"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:drawableLeft="@drawable/checkmark"

android:gravity="center_vertical"

android:textStyle="bold"

android:textSize="24dip"

android:maxLines="1"

 

android:ellipsize="end"/>

如程序中所见我在xml中设置了 DrawableLeft。

我想在代码中改变drawable。

有什么方法可以使用代码为textview设置drawableLeft呢?

很容易想到使用

public void  setCompoundDrawables  (Drawable left, Drawable top, Drawable right, Drawable bottom);

或者

public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)

它们两个的区别是:

使用第一个方法:

类似调用方法如下:

1.XML中使用android:drawableLeft="@drawable/icon"2.代码中动态变化

Drawable drawable= getResources().getDrawable(R.drawable.drawable);/// 这一步必须要做,否则不会显示.

drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());

myTextview.setCompoundDrawables(drawable,null,null,null);

注意必须调用setBounds,才能腾出空间来显示drawableLeft的图片

以上代码等效于:

myTextview.setCompoundDrawablesWithIntrinsicBounds (Drawable left,

Drawable top, Drawable right, Drawable bottom)

 

setCompoundDrawables使用:

Drawable drawable = getResources().getDrawable(R.drawable.clock);
drawable.setBounds(0, 0, textView.getHeight(), textView.getHeight());//后两个参数为图标的高宽
 textView.setCompoundDrawables(  null,null, drawable, null);//设置右图标

 

//隐藏右图标

Drawable drawable = getResources().getDrawable(R.drawable.clock);
drawable.setBounds(0, 0, 0, 0);
 textView.setCompoundDrawables(  null,null, drawable, null);
text1.setCompoundDrawables(  null,null, drawable, null);


如何在代码中动态为TextView设置drawableRight

牛若亦 | 浏览 1457 次

推荐于2016-10-25 20:20:33 最佳答案

1

<TextView

android:id="@+id/Title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:drawableRight="@drawable/check_down"

android:gravity="center_vertical"

android:textSize="24dip"

android:maxLines="1"

android:ellipsize="end"/>

  我们写在xml的时候,都是这么写的。那代码呢?我们发现TextView他有一个方法

2

public void  setCompoundDrawables  (Drawable left, Drawable top, Drawable right, Drawable bottom);

  这个方法呢,就是可以在Java代码动态的画 左上右下几个方向

  类似于xml中的 android:drawableLeft="@drawable/icon"

android:drawableTop="@drawable/icon"

android:drawableRight="@drawable/icon"

android:drawableButtom="@drawable/icon"

3

  具体在代码中的用法是:

Drawable drawable = getResources().getDrawable(R.drawable.spinner_checked);

 

drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界

titleTv.setCompoundDrawables(null, null, drawable, null);//画在右边

注:getResources().getDrawable(R.drawable.spinner_checked)方法已经过时,我们可以用ContexCompat代替

ContextCompat.getDrawable(this, R.drawable.your_drawable) (Here this is the context)

ContextCompat.getDrawable(getActivity(), R.drawable.name);

 

 

 

 


 

 

 

 

15.

 

16.JSON数据解析

17.ImageView图片加载方式

setImageBitmap(Bitmap)

setImageDrawable(Drawable)

setImageResource(R.drawable.xxx)

setImageURI(Uri)

18.如何打点

19.ImageView设置监听

20.LayoutParam如何写

21.可以通过选中一个方法或者类,图片等右键然后Find usage 找到和它相关的资源

22.断点设置:

23.ViewsetVisiblity方法参数设置。

 

 

 

 

24.

 

 

 

25.

   安卓图片一直用的是720

26.

27.Android Studio断点调试

28.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值