【问题标题】:How to Make LinearLayout Clickable如何使 LinearLayout 可点击
【发布时间】:2015-02-07 07:04:12
【问题描述】:

我有一个这样的线性布局

<LinearLayout
    android:id="@+id/optionlist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/divider"
    android:orientation="vertical" >

</LinearLayout>

我想在这个布局中添加另一个 xml 视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/option"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!--
               <View 
               android:layout_width="match_parent"
               android:layout_height="0.5dip"
               android:layout_marginLeft="10dp"
                 android:layout_marginRight="10dp"
                android:background="#2fd275"
               />
    -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp" >

        <ImageView
            android:id="@+id/option_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/logout" />

        <TextView
            android:id="@+id/option_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#000000"
            android:visibility="gone" />

        <TextView
            android:id="@+id/option_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"
            android:text="Signout"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#616161" />
    </LinearLayout>

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="0.5dip"
        android:background="#d3d3d3" />

</LinearLayout>

通过代码

    for (int i = 0; i < opt.length; i++) {

        View view = inflater.inflate(R.layout.option_item, optionlist,
                false);
        view.setBackgroundResource(R.drawable.optionlist);
        view.setTag(opt_image[i]);
        view.setClickable(true);
        view.setFocusable(true);

        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // ADD your action here

                int res = (Integer) v.getTag();

                switch (res) {
                case R.drawable.logout:
                    signout();
                    break;
                }

            }
        });
        final TextView tv = (TextView) view.findViewById(R.id.option_name);
        final ImageView iv = (ImageView) view
                .findViewById(R.id.option_image);
        tv.setText(opt[i]);
        iv.setImageResource(opt_image[i]);
        optionlist.addView(view);
    }

现在单击布局时,我想加载此 xml 文件,如下所示...但我无法在单击事件上加载背景。请提出建议我做错了什么?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/holo_green_dark" android:state_focused="true"/>
    <item android:drawable="@color/holo_green_dark" android:state_enabled="false" android:state_pressed="true"/>
    <item android:drawable="@color/white"/>

</selector>

【问题讨论】:

    标签: android android-layout layout android-linearlayout


    【解决方案1】:

    首先从xml获取LinearLayout的id,然后对其执行onClickListener而不是整个View

    View view  = inflater.inflate(R.layout.option_item, optionlist, false);
    LinearLayout layout = view.findViewByIt(R.id.optionlist);
    layout.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
          // ADD your action here
          int res=(Integer) v.getTag();
          switch(res)
          {
             case R.drawable.logout: signout();
                                    break;
          }
       }
    });
    

    【讨论】:

      【解决方案2】:

      试试这个..希望它有效..

        <?xml version="1.0" encoding="utf-8"?>
         <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:state_pressed="true" 
              android:drawable="#ff0000" />//ur onclick desired color 
          <item  android:state_focused="false" 
              android:drawable="@android:color/transparent" />focused color
         </selector>
      

      只需创建新的 xml 并将此代码添加到 drawable 中,并将您的线性layoyut backgrout 设置为其名称,如android:background="@drawable/createdxmlname"

      <LinearLayout
              android:id="@+id/optionlist" 
              android:layout_below="@id/divider"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:background="@drawable/createdxmlname"
              android:clickable="true">
      

      【讨论】:

      • @MS Gadag 我已经有了可见的布局我想在其点击事件中更改布局的背景。
      • 在我的情况下是颜色,如果我想在释放点击时将其重置为正常状态。
      • 你什么时候想正常重置?
      • 点击后布局的颜色应该恢复正常..根据上面的建议,您给它的建议仍然是我们设置新颜色。我只是想让这些工作让用户知道它被点击了。
      • 如果您希望背景有波纹,并且不需要使用 API 级别 21,请在 XML 中的线性/相对/布局/视图中使用 android:background="?attr/selectableItemBackground"
      【解决方案3】:
         android:clickable="false"
      
      
       <LinearLayout  
            android:id="@+id/parent"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:orientation="horizontal">  
            <ImageButton  
                 android:clickable="false"  
                 android:layout_width="40dp"  
                 android:layout_height="40dp"  
                 android:text="Cinema"  
                 android:textSize="13sp"  
                 android:layout_marginRight="10dp" />  
            <Button  
                 android:clickable="false"  
                 android:layout_width="match_parent"  
                 android:layout_height="40dp"  
                 android:text="Check in"  
                 android:textSize="13sp" />  
       </LinearLayout>  
      

      【讨论】:

      • 谢谢!对我来说,它起作用了,我还将线性布局的属性标记为:android:clickable="true" android:focusable="true" 和你说的按钮:android:clickable="false"
      【解决方案4】:

      在drawable中创建一个pressed_layout.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/button_pressed" /> <!-- pressed -->
         <item android:state_focused="true"
            android:drawable="@drawable/button_focused" /> <!-- focused -->
         <item android:drawable="@drawable/button_normal" /> <!-- default -->
       </selector>
      

      并在您的布局中将其添加为背景资源:

      <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/pressed_layout"
        android:clickable="true">
      </LinearLayout>
      

      【讨论】:

        【解决方案5】:

        xml 文件中的layout.setclickble="true" 和

        layout.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View arg0) {
                    // TODO Auto-generated method stub
        
                }
            });  
        

        【讨论】:

          【解决方案6】:

          我们也可以在布局的XML中使用onClick,在java文件中使用该方法,如onClick="next",那么在java文件中我们必须使用该方法。

          public void next()
          {
          // do something
          } 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-12-15
            • 2011-05-02
            • 1970-01-01
            • 1970-01-01
            • 2013-03-07
            • 2019-08-05
            相关资源
            最近更新 更多