Java setBackgroundResource没有';行不通

Java setBackgroundResource没有';行不通,java,android,android-fragments,Java,Android,Android Fragments,好吧,我试图改变我的抽屉导航器每当我改变片段,我设法改变了动作栏的颜色和背景颜色,但问题是,有背景是不够的。。。我看到我用其他颜色声明了一个BackgroundResource,当我尝试更改颜色时,它不起作用 My MainActivity.xml <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+i

好吧,我试图改变我的抽屉导航器每当我改变片段,我设法改变了动作栏的颜色和背景颜色,但问题是,有背景是不够的。。。我看到我用其他颜色声明了一个BackgroundResource,当我尝试更改颜色时,它不起作用

My MainActivity.xml

<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">
    <!-- Displaying Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <!-- Displaying Drawer -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"        
        android:listSelector="@drawable/drawer_list_selection"
        android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>

我知道我做错了什么,但我不明白。。。您能解释一下每次更改片段时如何更改此选项吗?

然后您要做的是创建一个可绘制选择器->list\u selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/list_background2" android:state_pressed="true"/>
    <item android:drawable="@color/list_background" />
</selector>
如果您想在片段中每次发生某件事情时都执行此操作,您需要创建一个接口:

public iterface TalkToActivity(){
  public void sendChangeEvent(int changeType);
}
在片段中创建一个局部变量:

TalkToActivity m_callBack;
然后在片段的onAttach方法中:

    @Override
public void onAttach(Activity activity) {
    // Call to the Super Class
    super.onAttach(activity);

    // Attempt to Add the Interface
    try {

        m_callBack = (TalkToActivity) activity;

    } catch (ClassCastException e) {

        // Print to LogCat if the Parent Did not implement the interface
        Log.e(FRAGMENT_TAG, "Failed to implement interface in parent.", e);
    }
}
然后,如果您希望捕获并传达更改:

public void buttonWasPressed(int changeType){
     if(m_callBack != null){
         m_callBack.sendChangeEvent(changeType);
     }
}
最后,在您的活动中,使您的活动“Implients TalkToActivity”,这将强制您重写此方法中的sendChangeEvent和方法

@Override
public void sendChangeEvent(int changeType){
 switch(changeType){
    case 0: 
            // Update you UI like above
            // ...
            if(mDrawerList != null){
                mDrawerList.setSelector(R.drawable.list_selector);
            }
       break;
    default: break;
  }

}
注意:如果您有list_item.xml和适配器:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/linearLayoutItem"
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:orientation="vertical">
   <TextView
       android:id="@+id/textViewNavItem"
       android:layout_height="wrap_content"
       android:layout_width="match_parent"/>
 </LinearLayout
用上述方法动态地改变背景

linearLayoutItem.setBackground(R.drawable.list_selector);
但是创建6个不同的列表选择器num1.xml等。。。并使用switch语句更改此值。但是您需要适配器具有如下方法

 public void updateViewBackground(Drawable drawable) {}
它将在适配器中执行此操作

有关片段和活动之间通信的演示,请参见:


片段通过bundle和onPause()与主活动对话


试试setBackgroundColor(Color.BLUE)和mActionBar.setBackgroundDrawable(新的ColorDrawable(0xff00DDED));背景这是一个对我有用的部分,我想改变的是我的ListItems的背景,我的ListItems(选中)让我知道它是否有效work@X-这不是我想做的,请阅读我的答案。我在我的colors.xml上创建了6个项目吗?为什么你在第一个项目上放了
android:state\u pressed=“true”/>
,这会告诉ListView当它处于按下状态时使用这个,但我想要的是更改我的ListView项目的背景,我只更改了抽屉导航的背景,而没有更改ListView的背景,也没有更改所选项目。你的list\u item.xml是什么?你真的应该动态地改变背景,而不是仅仅改变你的选择。这不是最好的方法,请参阅我上面的答案,通过界面进行交流。
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/linearLayoutItem"
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:orientation="vertical">
   <TextView
       android:id="@+id/textViewNavItem"
       android:layout_height="wrap_content"
       android:layout_width="match_parent"/>
 </LinearLayout
 LinearLayout linearlayoutItem = (LinearLayout) findViewById(R.id.linearLayoutItem);
linearLayoutItem.setBackground(R.drawable.list_selector);
 public void updateViewBackground(Drawable drawable) {}