【问题标题】:Changing Linear layout background color while clicking单击时更改线性布局背景颜色
【发布时间】:2013-04-12 05:44:36
【问题描述】:

我的 xml 中有一个线性布局,并正在通过 java 向该线性布局添加另一个线性布局(包含两个文本视图)。触摸事件完美无缺,但我想通过设置背景颜色突出显示选定的线性布局。请指教。

【问题讨论】:

  • 发布你尝试过的代码
  • LinearLayouts 实现 Touch 或 Click 事件,并根据布局的选择更改其背景颜色。
  • Raghunandan 有很好的答案 - 你应该选择它

标签: android


【解决方案1】:

在drawable文件夹中定义background.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/pressed" />
<item  android:state_focused="false" 
    android:drawable="@drawable/normal" />
</selector>

drawable 文件夹中的 normal.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>    
</shape>

drawable 文件夹中的pressed.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FF1A47"/>      
</shape>

然后为你的布局设置背景

  android:background="@drawable/background"

你也可以如下设置背景

触摸你的布局

  ll.setOnTouchListener( new View.OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch(event.getAction())
            {
        case MotionEvent.ACTION_DOWN:
            ll.setBackgroundColor(Color.RED);
            break;
            case MotionEvent.ACTION_UP:

            //set color back to default
            ll.setBackgroundColor(Color.WHITE);  
            break;
            }
            return true;        
        }
    });

【讨论】:

    【解决方案2】:

    无论您选择何种方法(XML/代码)- 确保您的 LinearLayout 可点击:

    XML:

    android:clickable="true"
    

    代码:

    myLinearLayout.setClickable(true);
    

    【讨论】:

      【解决方案3】:

      这是我的解决方案,很简单:

      <LinearLayout
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:background="@drawable/bg_pressed_tab"
                  android:clickable="true"
                  android:focusable="true"
                  android:focusableInTouchMode="true"
                  android:gravity="center"
                  android:orientation="vertical">
      
                  <ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:contentDescription="@string/app_name"
                      android:src="@mipmap/ic_launcher" />
      
                  <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/service_request"
                      android:textColor="@color/white"
                      android:textSize="@dimen/text_size_small" />
      
                  <requestFocus />
              </LinearLayout>
      

      bg_tab_pressed.xml

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

      【讨论】:

        【解决方案4】:

        将 selector.xml 文件放入 drawable 文件夹(res/drawable)

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

        并在 xml 文件中设置线性布局的背景

        android:background="@drawable/background"
        

        【讨论】:

        • 不正确,你不能命名文件 selector.xml 然后调用 @drawable/background 并期望它工作
        【解决方案5】:

        以下方法将得到你想要的。甚至是 Lollipop 中的波纹动画。只需将上下文传递给视图(可以是线性布局):

        public static void setClickableAnimation(final Context context, final View view) {
            final int[] attrs = new int[]{R.attr.selectableItemBackground};
            final TypedArray typedArray = context.obtainStyledAttributes(attrs);
            final int backgroundResource = typedArray.getResourceId(0, 0);
            view.setBackgroundResource(backgroundResource);
            typedArray.recycle();
        }
        

        【讨论】:

          【解决方案6】:

          试试View函数setBackgroundColor()

          查看 Android 文档:

          http://developer.android.com/reference/android/view/View.html#setBackgroundColor%28int%29

          public void setBackgroundColor (int color)       Added in API level 1
              Sets the background color for this view.
          Parameters
              color   the color of the background 
          

          【讨论】:

            猜你喜欢
            • 2023-03-12
            • 2015-03-29
            • 2018-09-15
            • 1970-01-01
            • 2019-09-01
            • 2014-06-12
            • 2014-10-09
            • 2011-06-05
            • 2013-09-27
            相关资源
            最近更新 更多