setBackgroundResource 不起作用
setBackgroundResource doesn't work
好吧,每当我更改片段时,我都会尝试更改我的抽屉导航器,我设法更改了 ActionBar 颜色和背景颜色,但问题是背景是不够的...我看到了我用其他颜色声明了 BackgroundResource,当我尝试更改颜色时它不起作用。
我的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>
我的 colors.xml(我试过手动更改它,但我不知道该怎么做,然后我创建了与我的可绘制导航器上项目相同的颜色)。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="list_item_title">#fff</color>
<color name="list_background">#458A79</color>
<color name="list_background2">#ffc591</color>
<color name="list_background3">#ab91ff</color>
<color name="list_background4">#f784fe</color>
<color name="list_background5">#91dfff</color>
<color name="list_background_pressed">#6FA698</color>
<color name="list_background_pressed2">#ffc591</color>
<color name="list_background_pressed3">#ab91ff</color>
<color name="list_background_pressed4">#f784fe</color>
<color name="list_background_pressed5">#91dfff</color>
<color name="list_divider">#fff</color>
<color name="counter_text_bg">#626262</color>
<color name="counter_text_color">#c5c4c4</color>
</resources>
我在 MainActivity.java 上试过的是
private void displayView(int position) {
// update the main content with called Fragment
Fragment fragment = null;
LlistaGenericaFragment frag = null;
FragmentTransaction ft = null;
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#256F5C")));
switch (position) {
case 0:
fragment = new MetallsAlcalinsFragment();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffc591")));
mDrawerList.setBackground(new ColorDrawable(Color.parseColor("#ffc591"))); //set the background but not the ListView
mDrawerList.setBackgroundResource(R.color.list_background2); //Don't see any change
break;
我知道我做错了什么,但我不明白...你能解释一下我每次更改片段时如何更改它吗?
然后您要做的是创建一个可绘制选择器 -> list_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>
然后在您的列表中将其添加为选择器:
mDrawerList.setSelector(R.drawable.list_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);
}
}
最后,在您的 activity 中创建 activity 'implents 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
然后获取对父项的引用
LinearLayout linearlayoutItem = (LinearLayout) findViewById(R.id.linearLayoutItem);
并使用上述方法动态更改其背景...
linearLayoutItem.setBackground(R.drawable.list_selector);
但创建 6 个不同的 list_selector_num1.xml 等...并使用 switch 语句更改它。但是你需要适配器有一个像
这样的方法
public void updateViewBackground(Drawable drawable) {}
这将在您的适配器中执行此操作。
有关 Fragment 与活动之间的这种通信的演示,请查看:
片段通过 bundle 和 onPause()
与主 activity 对话
Passing data between a fragment and its container activity
好吧,每当我更改片段时,我都会尝试更改我的抽屉导航器,我设法更改了 ActionBar 颜色和背景颜色,但问题是背景是不够的...我看到了我用其他颜色声明了 BackgroundResource,当我尝试更改颜色时它不起作用。
我的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>
我的 colors.xml(我试过手动更改它,但我不知道该怎么做,然后我创建了与我的可绘制导航器上项目相同的颜色)。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="list_item_title">#fff</color>
<color name="list_background">#458A79</color>
<color name="list_background2">#ffc591</color>
<color name="list_background3">#ab91ff</color>
<color name="list_background4">#f784fe</color>
<color name="list_background5">#91dfff</color>
<color name="list_background_pressed">#6FA698</color>
<color name="list_background_pressed2">#ffc591</color>
<color name="list_background_pressed3">#ab91ff</color>
<color name="list_background_pressed4">#f784fe</color>
<color name="list_background_pressed5">#91dfff</color>
<color name="list_divider">#fff</color>
<color name="counter_text_bg">#626262</color>
<color name="counter_text_color">#c5c4c4</color>
</resources>
我在 MainActivity.java 上试过的是
private void displayView(int position) {
// update the main content with called Fragment
Fragment fragment = null;
LlistaGenericaFragment frag = null;
FragmentTransaction ft = null;
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#256F5C")));
switch (position) {
case 0:
fragment = new MetallsAlcalinsFragment();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffc591")));
mDrawerList.setBackground(new ColorDrawable(Color.parseColor("#ffc591"))); //set the background but not the ListView
mDrawerList.setBackgroundResource(R.color.list_background2); //Don't see any change
break;
我知道我做错了什么,但我不明白...你能解释一下我每次更改片段时如何更改它吗?
然后您要做的是创建一个可绘制选择器 -> list_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>
然后在您的列表中将其添加为选择器:
mDrawerList.setSelector(R.drawable.list_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);
}
}
最后,在您的 activity 中创建 activity 'implents 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
然后获取对父项的引用
LinearLayout linearlayoutItem = (LinearLayout) findViewById(R.id.linearLayoutItem);
并使用上述方法动态更改其背景...
linearLayoutItem.setBackground(R.drawable.list_selector);
但创建 6 个不同的 list_selector_num1.xml 等...并使用 switch 语句更改它。但是你需要适配器有一个像
这样的方法 public void updateViewBackground(Drawable drawable) {}
这将在您的适配器中执行此操作。
有关 Fragment 与活动之间的这种通信的演示,请查看:
片段通过 bundle 和 onPause()
与主 activity 对话Passing data between a fragment and its container activity