Android_Resource/Activity的一些小知识点

setLayoutParams

textView.setLayoutParams(new TextSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

为什么要用TextSwitcher的LayoutParams呢。查一查API,可以看到这么一句话These supply parameters to theparent of this view specifying how it should be arranged。也就是说一定要用父控件的LayoutParams。如果父控件是LinearLayout,当然就必须写成LinearLayout.LayoutParams


FrameLayout的android:foreground属性可以设置单击时的前景色

触摸之后,设置前景色,可以告诉用户确实单击了,增加用户体验。

<FrameLayout android:foreground="@drawable/pressed_backgorund_corner" >
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:state_pressed="true">
        <shape>
	    	<solid android:color="@color/defaultFocusAlphaColor"/>
            <corners android:radius="3dip" />
        </shape>
    </item>
	<item android:drawable="@color/nocolor" />
</selector>

这样,touch事件触发的时候,就会有前景色了。


findViewById之外的另一种获得控件的方法public View getChildAt(int index)

比如有这样一个布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>
我们没有为2个TextView设置ID,但是照样可以得到它们,方法如下
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
TextView tv1 = (TextView) linearLayout.getChildAt(0);
TextView tv2 = (TextView) linearLayout.getChildAt(1);


关于子控件和父控件的事件响应问题

当父控件中有子控件的时候,并且父控件和子空间都有事件处理(比如单击事件)。这时,点击子控件,父控件的单击事件就无效了。如下图:

比如一个LinearLayout里面有一个子控件TextView,但是TextView的大小没有LinearLayout大

①如果LinearLayout和TextView都设置了单击事件,那么

  • 点击TextView区域的时候,触发的是TextView的事件,
  • 点击TextView以外的区域的时候,还是触发的LinearLayout的事件。

②如果LinearLayout设置了单击事件,而TextView没有设置单击事件的话,那么

  • 不管单击的是TextView区域,还是TextView以外的区域,都是触发的LinearLayout的单击事件

如果LinearLayout的大小和TextView一样的话,那么

①如果LinearLayout和TextView都设置了单击事件,那么

  • 只有TextView的单击事件有效

②如果LinearLayout设置了单击事件,而TextView没有设置单击事件的话,那么

  • 触发的是LinearLayout的单击事件


XML文件中的@和?的区别

@大家已经司空见惯。比如@+id/...  @string/...

?是什么呢,看下面代码:

    <ProgressBar
        android:id="@+id/firstProgressBar"
        style="?android:progressBarStyleHorizontal"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:visibility="gone" />
上面代码中用到了“?”,那么“?”是什么意思呢?

“?”引用主题属性,当您使用这个标记,你所提供的资源名必须能够在主题属性中找到,因为资源工具认为这个资源属性是被期望得到的,您不需要明确的指出它的类型(也就是不需要写全在哪个文件中?android:attr/android:textDisabledColor)

那么什么又叫主题属性呢?

你可以在Android的SDK的以下目录中找到attrs.xml文件

D:\android-sdk\platforms\android-16\data\res\values

打开这个文件可以看到,Android系统的属性都定义在这个文件中。

举个例子(ImageView):

    <declare-styleable name="ImageView">
        <!-- Sets a drawable as the content of this ImageView. -->
        <attr name="src" format="reference|color" />
        <!-- Controls how the image should be resized or moved to match the size
             of this ImageView. -->
        <attr name="scaleType">
            <enum name="matrix" value="0" />
            <enum name="fitXY" value="1" />
            <enum name="fitStart" value="2" />
            <enum name="fitCenter" value="3" />
            <enum name="fitEnd" value="4" />
            <enum name="center" value="5" />
            <enum name="centerCrop" value="6" />
            <enum name="centerInside" value="7" />
        </attr>
        <!-- Set this to true if you want the ImageView to adjust its bounds
             to preserve the aspect ratio of its drawable. -->
        <attr name="adjustViewBounds" format="boolean" />
        <!-- An optional argument to supply a maximum width for this view.
             See {see android.widget.ImageView#setMaxWidth} for details. -->
        <attr name="maxWidth" format="dimension" />
        <!-- An optional argument to supply a maximum height for this view.
             See {see android.widget.ImageView#setMaxHeight} for details. -->
        <attr name="maxHeight" format="dimension" />
        <!-- Set a tinting color for the image -->
        <attr name="tint" format="color" />
        <!-- If true, the image view will be baseline aligned with based on its
             bottom edge -->
        <attr name="baselineAlignBottom" format="boolean" />
         <!-- If true, the image will be cropped to fit within its padding -->
        <attr name="cropToPadding" format="boolean" />
    </declare-styleable>

以上是从attrs.xml文件中提取的,可以看到ImageView的一些特有属性(由于ImageView继承自View,所以View的属性自然就继承了)。同样,Android系统也为Theme定义了很多属性(可以看看attrs.xml文件),其中每个主题属性的名称都一一对应D:\android-sdk\platforms\android-16\data\res\values目录下的themes.xml文件。当要用到主题属性的时候,就不需要特别指定android:attr/,可以直接在?后面加上属性名。


资源文件夹res/raw和asserts的异同

相同点:两个目录下的文件在打包后都会原封不动的保存到apk中,不会被编译成二进制。

不同点:

  1. res/raw中的文件会被映射到R.java中,访问的时候直接使用资源ID即R.id.filename;assets文件夹下的文件不会被映射到R.java中,访问的时候需要AssetManager类
  2. res/raw不可以有目录结构,而assets则可以有目录结构,也就是assets目录下可以再建立文件夹

读取res/raw下的资源代码:

InputStream is = getResources().openRawResource(R.id.filename); 

读取asserts下的资源代码:

AssetManager am = getAssets();

InputStream is = am.open("filename");

另外:res/raw和assets文件大小都有限制(有些资料说高版本的Android系统取消了这种限制),默认最大仅支持1M的文件,否则apk程序将报错。如果AssetManager或Resources classes方法来获取InputStream,将抛出java.io.IOException的异常如下DEBUG/asset(1123): Data exceeds UNCOMPRESS_DATA_MAX。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值