linearLayout等布局添加动态背景(运用xml)注意加上clickable

</pre><p><pre name="code" class="html">

博主这些天在写一个app社交软件,开发过程中奇葩问题也是各种乱入,这个也算是其中之一,主要以前不用layout布局做按钮类,所以也不会做它的背景。话不多说,上代码:

这是布局文件:

<pre name="code" class="html"><LinearLayout
        android:id="@+id/ll_photo_local_get"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@drawable/tv2_selector"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="现在拍摄"
            android:textColor="@color/deepgrey"
            android:textSize="18sp" />
    </LinearLayout>


 

这是tv1_selector选择器:

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

</selector>

然后怎么也出不来这个效果:


本来感觉根本不会出问题的,后来在网上查了才知道layout都有一个clickable属性,否则点击是无效的

更改布局文件:

<LinearLayout
        android:id="@+id/ll_photo_local_get"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:clickable="true"          //这里为新增
        android:background="@drawable/tv2_selector"
        android:orientation="vertical" >


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="现在拍摄"
            android:textColor="@color/deepgrey"
            android:textSize="18sp" />
    </LinearLayout>


这样就有该有的效果了。希望能帮助一些还会碰上这个问题的人吧!~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值