【问题标题】:How change the background color of focussed view如何更改焦点视图的背景颜色
【发布时间】:2011-05-21 02:27:11
【问题描述】:

我想改变焦点下视图的颜色;在我的例子中,它是一个表格行。

这是我当前的代码,但它不起作用。

            r.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1) {
                {               {
                    arg0.setBackgroundColor(Color.LTGRAY);
                    r.setFocusableInTouchMode(true);
                }
            return false;
        }});

【问题讨论】:

  • 使用此代码实际发生了什么
  • 对不起.. 看看编辑过的.. 这样做之后,一旦我点击它,整行就会改变颜色.. 在选择多行时,每行都会保持这种颜色.. 我尝试使用 isFocused( ) 在 if 条件下.. 不起作用
  • 我不确定,但根据我的说法,tablerow 的颜色不会在焦点上改变。你可以使用 ListView 来实现它
  • 但你确定 listView 可以达到 tablerow 的目的..

标签: android


【解决方案1】:

你可以使用 XML drawable,
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

创建一个这样的 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>

你需要背景图片

【讨论】:

  • k 谢谢.. 让我看看这是否可行.. 上面的 xml 文件应该是手动粘贴的 res 目录我猜...?
  • 如果它对你有用,你应该接受答案,或者回复你得到的输出。如果这个答案不起作用,我们也可以通过其他方式帮助你:)
  • 周围的每个人都提出了相同的答案.. 但它有点困难的过程,因为你需要有 android 文件系统的明确信息.. 它不仅仅是编码.. 我是一个新鲜的而不是完美..仍在努力..让你知道如果我得到它..
【解决方案2】:

这里我给你举个例子…… http://life-as-a-coder.com/_shared/background_state.rar

看看我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="@drawable/item_background"
        android:clickable="true">
        <TextView
            android:text="Item 1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35dip"></TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="@drawable/item_background"
        android:clickable="true">
        <TextView
            android:text="Item 2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35dip"></TextView>
    </LinearLayout>
</LinearLayout>

我在这个例子中使用了 LinearLayout。我设置android:clickable="true"激活点击状态... 这也可以在 ListView 中使用...只需在自定义行 Item 上使用它...

抱歉我的英语语法不好

【讨论】:

    猜你喜欢
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 2018-04-14
    • 2018-06-11
    • 2013-02-27
    • 2011-09-10
    相关资源
    最近更新 更多