2012-01-24 9 views
-2

私のアプリには、「レイアウト」の背景にどのような色を設定するかを選択できる「設定」というメニューボタンがあります。残念ながら、私は2つのレイアウトを組み合わせて、私が見せたいものを表示しています。問題は、レイアウトにある背景色だけを設定できますが、他のレイアウトにはTextViewの色を設定できないことです。Android - 共有プレビューと一緒に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> 

MainActivity:

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の色も変更したいと思います。私は一緒に色を変更したいと思います(例:背景の黒色、テキストの白い色、背景の白い色、テキストの黒い色など)。どうすればいいですか?答えることができるすべての人に感謝します。

答えて

0

私が最初に答えたとき、私はPreferenceActivity、PreferenceManager、またはあなたの質問を理解したとは思わない。あなたの問題は、あなたの好みを保存するのではなく、あなたのリストをスタイリングすることですね。

アクティビティのテーマを設定することが解決策になると思います。 This postテーマの変更時にテキストの色を更新する方法について説明しています。おそらくテーマについての読書を開始する場所はStyles and Themesでしょう。

+0

動作しません。 – Carontes

関連する問題