English 中文(简体)
从奥兰治改为格林岛
原标题:Change Selection in a ListView from Orange to Green

我如何在选定的每个项目上这样做。

我尝试将此添加到android:background上。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" 
        android:state_pressed="false"
        android:drawable="@color/android_green" />
    <item android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:drawable="@color/white_alpha" />
</selector> 

但它没有工作,它改变了整个名单。


在这里,我至今还没有工作。

• 清单调查

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/black_alpha" />
    <item android:drawable="@color/white_alpha" />  
</selector> 

• 采用上述观点

<ListView android:id="@+id/list" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_below="@+id/round"
    android:listSelector="@drawable/listview_background">
</ListView>

问题是,黑体字母将适用整个清单,而不仅仅是选定的清单项目。

问题回答

http://android-journey.blogspot.com/2009/12/android-selectors.html

我不想把它定为名单上的<代码>android:background,但我认为你希望建立android:listSelector。 如下所示:

<ListView android:id="@+id/list" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:layout_gravity="center"
      android:divider="@null" 
      android:dividerHeight="0dip"
      android:listSelector="@drawable/list_selector" />

您说:

问题是,黑体字母将适用整个清单,而不仅仅是选定的清单项目。

我有同样的问题。 在与它一起玩.后,我得以利用我所希望的彩色的3x3平方形象,而不是使用一种颜色,停止将彩色应用到整个清单中。 因此,如果你使用一个颜色,你会获得奇迹的背景,如果你使用一个猪的话,那就行了。 :

因此,这是解决这一问题的一种途径。

- 另一种方式

在我确定名单时,我发现最佳结果是,没有这样的国家:

页: 1

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

然后,我在清单项目背景下照顾所有国家。 NOT in the selectedor. 我发现我对此有了更多的控制。

第/栏/清单:项目_bg.xml

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

下面是我的清单项目:

转/延期/简单_项目_xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingRight="45dip"
android:paddingBottom="2dip"
android:layout_gravity="center_vertical"
android:background="@color/list_item_bg">
  <ImageView
    android:id="@+id/arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dip"
    android:src="@drawable/list_item_arrow" />
 <TextView
android:id="@+id/simple_list_item_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="12sp"
android:textColor="@color/list_label_color"/>
</LinearLayout>

我也面临你的问题,即对整个名单的肤色变化适用。

但现在它为我工作:

我的主页载有清单(原名我在此不适用名单):

 <ListView 
    android:id="@+id/controlList" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

相反,使用名单者获取意见,如,即特定行文(斜体字),在国家变化(如压力)时,使用名单人xml,可以编造:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null){
                LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                convertView = li.inflate(R.layout.control_list, null);
                viewHolder = new ViewHolder();

                viewHolder.topText = (TextView) convertView.findViewById(R.id.toptext);
                viewHolder.bottomText = (TextView) convertView.findViewById(R.id.bottomtext);
                convertView.setTag(viewHolder);

            }
            else{
                viewHolder = (ViewHolder) convertView.getTag();
            }


                    //each convertView will be assigned the listselector
            convertView.setBackgroundResource(R.drawable.listselector);


            viewHolder.topText.setText(testSchemes[position]);

            //viewHolder.vText.setText(testDetails[position]);
            return convertView;
        }

最后,我的名单是:

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false"
        android:state_selected="false"
        android:drawable="@color/lightgreen" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_pressed="true" 

        android:drawable="@color/darkgreen" />

    <item android:state_selected="true" android:state_pressed="false"
        android:drawable="@color/orange" />



</selector>

This is what the above code does: All entries when first loaded are lightgreen. Scrolling up and down using cursor highlights the selected entry orange. Pressing a row turns it to darkgreen.

正如评论中提到的大石一样,一项工作解决办法正在使用一种形状而不是一种颜色(我也看到整个清单得到强调)。 这里的解决办法是:

在清单中添加这2个特性:

android:drawSelectorOnTop="true"
android:listSelector="@drawable/my_list_selector"

添加我从名单上删除的_子:

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

现在,清单项目应显示压力状态的半透明绿色超支。

我也面临同样的问题,然后我只需要一个简单的一线来看待xml。

    android:background="?android:attr/activatedBackgroundIndicator"

核对这一员额: 这个员额可帮助





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签