android中LinearLayout包含TextView与ImageView的selector效果与include用法

原博主:http://blog.sina.com.cn/s/blog_79aa89690101b1w5.html

 这篇文章主要讲下关于LinearLayout包含TextView与ImageView(可能是其中一个可能2个控件都有)种使用selector的总结与错误分析,希望可以帮助自己与需要的人。

 一、最近自己要做一个导航条,直接给图:

android中LinearLayout包含TextView与ImageView的selector效果与include用法

 

二、先说一下效果:点击<发现>按钮出现 图片与文字《发现》一起改变(效果时间太短,不好拍照只会自己作图了)。

android中LinearLayout包含TextView与ImageView的selector效果与include用法

 

三、好了,效果我们确定了,那接下来上代码(让代码说话):

 

foot_include.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@id/layoutFooter"
    style="@style/TailBar"
    android:gravity="center"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@id/frameDiscoverType"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="2.0dip"
        android:layout_marginRight="2.0dip"
        android:layout_weight="1.0"
        android:paddingTop="10.0dip" >

        <LinearLayout
            android:id="@+id/faxian"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"

            android:orientation="vertical" >

            <ImageView
                android:id="@id/footDiscoverIcon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:duplicateParentState="true"
                android:src="@drawable/sel_foot_discover"
 />

          <TextView
                  android:id="@id/footDiscoverText"
                style="@style/foot_style"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:duplicateParentState="true"
                android:text="@string/foot_discover_type"
                android:textColor="@drawable/sel_foot_discover_text" />
        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@id/frameNearFind"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="2.0dip"
        android:layout_marginRight="2.0dip"
        android:layout_weight="1.0"
        android:paddingTop="10.0dip" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@id/footNearIcon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/foot_nearfind_bg" />

            <TextView
                android:id="@id/footNearText"
                style="@style/foot_style"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/foot_near_find" />
        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@id/frameFreeFlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="2.0dip"
        android:layout_marginRight="2.0dip"
        android:layout_weight="1.0" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@id/footFreeFlayIcon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/foot_free_play_bg" />

            <TextView
                android:id="@id/footFreeFlayText"
                style="@style/foot_style"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/foot_free_flay" />
        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@id/frameShequ"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="2.0dip"
        android:layout_marginRight="2.0dip"
        android:layout_weight="1.0"
        android:paddingTop="10.0dip" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@id/footShequIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/foot_shequ_nomal" />

            <TextView
                android:id="@id/footShequText"
                style="@style/foot_style"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/foot_shequ" />
        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@id/frameMore"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="2.0dip"
        android:layout_marginRight="2.0dip"
        android:layout_weight="1.0"
        android:paddingTop="10.0dip" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@id/footPersonalIcon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/sel_foot_more_bg" />

            <TextView
                android:id="@id/footPersonalText"
                style="@style/foot_style"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/poi_detail_foot_more" />
        </LinearLayout>
    </FrameLayout>

</LinearLayout>

 其中代码中的 style也给出来:

styles.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="foot_text_style">(正确的写法)
        <item name="android:textSize">16.0sp</item>    
        <item name="android:gravity">center</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
    </style>

<style name="foot_style">(错误的写法,应删去红色部分)
        <item name="android:textSize">12.0sp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:gravity">center</item>
        <item name="android:paddingTop">0.0dip</item>
    </style>

   <style name="TailBar">
               <item name="android:background">@drawable/foot_bg</item>
               <item name="android:paddingTop">3.0dip</item>
               <item name="android:layout_width">fill_parent</item>
               <item name="android:layout_height">wrap_content</item>
               <item name="android:tileMode">repeat</item>
       </style>


</resources>

四、下面就是本章的关键selector的写法:

selector的用法我就不多写了,那直接上代码:

sel_foot_discover.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
   xmlns:android=" http://schemas.android.com/apk/res/android ">
     <item android:state_pressed="true" android:drawable="@drawable/foot_discover_bg_focus" />
       <item android:drawable= "@drawable/foot_discover_bg" />
</selector>

 

sel_foot_discover_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
   xmlns:android=" http://schemas.android.com/apk/res/android ">
 
           <!--获得焦点时的字体颜色-->
             <item android:state_pressed="true" android:color="@color/yellow"></item>
               <!-- 默认时的字体颜色--> 
       <item android:color="@color/white"></item>
</selector>

五、好,这里说一下:

有很多人都在问为什么我的效果就是出不来呢?

其实,是大家粗心了,粗心在哪里呢?

1、大家看我上面的layout的代码,里面有style元素,你看看foot_text_style与foot_style的区别没,就是foot_style设定了颜色,请问你style都设死了颜色,你叫selector怎么帮你变色啊。。

所以第一个检查的就是style元素不要设置颜色,而是由selector去设置基本颜色。

2.写selector出现错误:

因为你是在父类中包含内部控件的,如FrameLayout内含LinearLayout,LinearLayout内含有ImageView
与textview,你要点击LinearLayout使ImageView与textview同时改变,那selector写法中只能用android:state_pressed="true"元素,为什么不能用其他的设置,这个我目前也不知道,先把原因放这里。

3.在layout文件中父类中加上三中红色部分:

<LinearLayout

.

.

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

>

 <ImageView

.

.

android:duplicateParentState="true"
android:src="@drawable/sel_foot_discover"(我们写的selector文件)
/>

<TextView

.

.

android:duplicateParentState="true" (指的是当前控件是否跟随父控件的(点击、焦点等)状态
android:textColor="@drawable/sel_foot_discover_text" (我们写的selector文件)
/>


六、当然,如果只是这样简单的布局那还好,如果说我用include把这个布局加到别的xml文件中呢?

上图:

android中LinearLayout包含TextView与ImageView的selector效果与include用法

看到里面的include-foot_include就是刚才我所写的layout文件,这里说一下,嵌入include的写法

<LinearLayout android:id="@id/layoutBottom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true">
        <include layout="@layout/foot_include" />
    </LinearLayout>

那问题来了,在include引用中还能实现我们要的效果吗?答案是可以的,嵌入后效果还是有的,啰嗦一下,千万要记得不要给引用include部分上加上ID,如:

<include android:id="xxx" layout="@layout/foot_include" />这样会给你写JAVA代码带来麻烦

七、那我想点击foot_include.xml中的id为faxian的LinearLayout控件来实现页面的跳转,JAVA代码怎么写:

这里也给出代码,(其实也是为了怕自己忘记所写):

先定义

 private LinearLayout faxian;

后直接找到

faxian=(LinearLayout)findViewById(R.id.faxian);

 

faxian.setOnClickListener(new View.OnClickListener() {

             public void onClick(View v) {
                 // TODO Auto-generated method stub
               Intent it = new Intent();
                     it.setClass(mContext, AboutActivity.class);
                     startActivity(it);
             }
         });


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值