【发布时间】:2012-01-26 17:06:57
【问题描述】:
在我的应用程序中,我有一个名为“设置”的菜单按钮,我可以在其中选择为某些布局的背景设置的颜色。不幸的是,我将两个布局组合在一起以显示我想要显示的内容,问题是我只能设置背景颜色(即在布局中),但不能设置 TextView 的颜色(即在另一个布局中)。
布局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"
android:id="@+id/main_layout">
<EditText
android:id="@+id/editText1"
android:layout_height="65dip"
android:layout_marginTop="2dip"
android:hint="Scrivi"
android:layout_width="fill_parent" />
<ListView android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fastScrollEnabled="true">
</ListView>
</LinearLayout>
布局 listitem_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_layout">
<TextView
android:id="@+id/textView1"
android:text="TextView"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent">
</TextView>
<TextView
android:text="TextView"
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
主活动:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class SearchCocktail extends Activity{
EditText ed;
ListView lview;
String[] first = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
String[] second = { "Uno", "Due", "Tre", "Quattro", "Cinque", "Sei", "Sette", "Otto", "Nove", "Dieci"};
int textlength = 0;
ArrayList<String> first_sort = new ArrayList<String>();
ArrayList<String> second_sort = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed = (EditText) findViewById(R.id.editText1);
lview = (ListView) findViewById(R.id.list);
lview.setAdapter(new MyCustomAdapter(first, second));
...........
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listitem_row, parent, false);
TextView textview = (TextView) row.findViewById(R.id.textView1);
TextView textview1 = (TextView) row.findViewById(R.id.textView2);
textview.setText(data_first[position]);
textview1.setText(data_second[position]);
return (row);
}
}
@Override
protected void onResume(){
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
String colorePref = prefs.getString(PreferencesFromXml.COLORE_PREF, PreferencesFromXml.COLORE_DEFAULT);
int coloreDiSfondo = Color.parseColor(colorePref);
findViewById(R.id.list).setBackgroundColor(coloreDiSfondo);
editor.commit();
}
}
PreferencesFromXml 活动:
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class PreferencesFromXml extends PreferenceActivity{
public static final String COLORE_DEFAULT = "#000000";
public static final String COLORE_PREF = "colore";
public static final String TITOLO_PREF = "titolo";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
Preference titoloPrefs = findPreference(TITOLO_PREF);
titoloPrefs.setSummary(prefs.getString(TITOLO_PREF, getString(R.string.titolo_custom)));
titoloPrefs.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
{
public boolean onPreferenceChange(Preference prefs, Object value)
{
prefs.setSummary((CharSequence) value);
return true;
}
});editor.commit();
}
}
使用我的代码,我可以更改布局 main.xml 的背景颜色,但我还想更改 listitem_row.xml 的 TextView 颜色。我想一起更改颜色(例如:背景为黑色,文本为白色,背景为白色,文本为黑色等)。我该如何进行?感谢所有可以回答的人。
【问题讨论】:
标签: android sharedpreferences background-color