Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更改选定和按下的listView项目的颜色_Android_Android Listview_Textview - Fatal编程技术网

Android 更改选定和按下的listView项目的颜色

Android 更改选定和按下的listView项目的颜色,android,android-listview,textview,Android,Android Listview,Textview,我正在努力定制我的listview 我更改了listView中文本视图的背景 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_list_item_txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?an

我正在努力定制我的listview

我更改了listView中文本视图的背景

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_list_item_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#000"
android:background="@drawable/list_selector_orange"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

从TextView中删除此行:

android:background="@drawable/list_selector_orange"
并将其放入ListView

<ListView
 .....
 android:listSelector"@drawable/list_selector_orange"
 .....
 />

之后,您选择的项目将更改颜色


希望我能为您提供帮助。

将android:duplicateParentState=“true”添加到您的文本视图中,这样它将获得其父视图的状态。

您将onClickListener设置为哪个视图?
文本视图
列表视图
?源代码将非常有用。您可以点击此[链接][1],希望这将帮助您解决问题。[1] :我为ListView设置了一个McClickListener。我添加了缺少的代码。希望你现在能猜出我的错误,是我干的。但结果是一样的。所选项目未着色。。。选择的状态是否错误?或者别的什么?你有listview项目中的按钮吗?我添加了listview缺少的代码。。。不,我没有按钮。。。ListView视图是按钮本身的完美解决方案。谢谢。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="#FF8000"
        android:startColor="#FF8000" />
</shape>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"
    android:listSelector="@drawable/list_selector_orange" />
private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    // update the main content by replacing fragments
    if(fragment != null){
        //fragmentManager.beginTransaction().remove(fragment).commit();
    }

    Bundle args;
    switch(position){
    case 1:
        fragment = new de.sebspr.app08.viewpager.PagerFragment();
        args = new Bundle();
        args.putInt(de.sebspr.app08.viewpager.PagerFragment.ARG_PLAYER_NUMBER, position);
        fragment.setArguments(args);
        break;
    case 2:
        fragment = new de.sebspr.app08.viewpager.PagerFragment();
        args = new Bundle();
        args.putInt(de.sebspr.app08.viewpager.PagerFragment.ARG_PLAYER_NUMBER, position);
        fragment.setArguments(args);
        break;
    case 4:
        fragment = new de.sebspr.app08.halle.FragHalle();
        break;
    }

    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mListTitles[position].getTitle());
    mDrawerLayout.closeDrawer(mDrawerList);
}
public View getView(int position, View convertView, ViewGroup parent){
    Item item = items[position];
    ViewHolder holder;

    if(convertView == null){
        holder = new ViewHolder();
        if(item.isSection()){
            convertView = inflater.inflate(R.layout.drawer_list_section, null);
            holder.txt = (TextView) convertView.findViewById(R.id.drawer_list_section_txt);

            convertView.setLongClickable(false);
            convertView.setClickable(false);
            convertView.setOnClickListener(null);

            tFace = Typeface.createFromAsset(context.getAssets(),
                    "fonts/Roboto-BoldCondensed.ttf");
            holder.txt.setTypeface(tFace);

            convertView.setTag(holder);
        } else {
            convertView = inflater.inflate(R.layout.drawer_list_item, null);
            holder.txt= (TextView) convertView.findViewById(R.id.drawer_list_item_txt);

            tFace = Typeface.createFromAsset(context.getAssets(),
                    "fonts/Roboto-Regular.ttf");
            holder.txt.setTypeface(tFace);

            convertView.setTag(holder);
        }
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txt.setText(item.getTitle());

    return convertView;     
}
android:background="@drawable/list_selector_orange"
<ListView
 .....
 android:listSelector"@drawable/list_selector_orange"
 .....
 />