Android-如何设置文本视图和背景的颜色以及共享参考
在我的应用程序中,我有一个名为“设置”的菜单按钮,在这个按钮中,我可以选择一些布局背景的颜色设置。不幸的是,我将两个布局组合在一起以显示我想要显示的内容,问题是我只能设置背景颜色(即布局中的背景颜色),而不能设置TextView的颜色(即另一个布局中的背景颜色) Layout main.xml:Android-如何设置文本视图和背景的颜色以及共享参考,android,sharedpreferences,background-color,Android,Sharedpreferences,Background Color,在我的应用程序中,我有一个名为“设置”的菜单按钮,在这个按钮中,我可以选择一些布局背景的颜色设置。不幸的是,我将两个布局组合在一起以显示我想要显示的内容,问题是我只能设置背景颜色(即布局中的背景颜色),而不能设置TextView的颜色(即另一个布局中的背景颜色) Layout main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.c
<?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>
通过我的代码,我可以更改layout main.xml的背景颜色,但我还需要更改listitem_row.xml的文本视图颜色。我想一起改变颜色(例如:黑色为背景,白色为文本,白色为背景,黑色为文本,等等)。我怎样才能继续?感谢所有能回答的人。我想我在第一次回答时不理解PreferenceActivity、PreferenceManager或您的问题。您的问题在于设置列表的样式,而不是存储您的首选项,对吗 我认为为活动设定主题可能是一个解决方案。讨论主题更改时如何更新文本颜色。可能是开始阅读主题的地方
<?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();
}
}
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();
}
}