2011-10-01 6 views
1

공유 환경 설정에 관한 모든 것을 이미 제자리에두고 내 활동 중 하나에서 logcat에서 이와 같은 공유 환경 설정 값을 검색 할 수 있습니다. 문자열 i = prefs.getString ("bgColor", "# f2345"); System.out.println (i);레이아웃에 공유 환경 설정 값을 설정하는 방법

그러나이 활동에서는 이와 같은 레이아웃을 사용하고 있습니다. SimpleCursorAdapter sca = new SimpleCursorAdapter (this, R.layout.country_row, c, from, to);
setListAdapter (sca); 지금은 이미 환경에서 점점 오전 값을 사용하여

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
android:orientation="horizontal">  

    <TextView android:id="@+id/year" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ff000099" 
android:background="#ffffff80" 
android:padding="10dp" 
android:textSize="16sp" 
android:text="1964"/> 

<TextView android:id="@+id/country" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textColor="#ffffff80" 
android:background="#ff000099" 
android:padding="10dp" 
android:textSize="16sp" 
android:text="Sweden"/> 

, 내가 예를 들어 배경 색상이나 글꼴 여기를 변경하려면이 같다 "country_row는"내 XML 레이아웃 파일입니다 크기가 표시됩니다. 이제 내가 원하는 것은 내 레이아웃 xml 파일에 이러한 공유 우선 순위 값을 암시하는 것입니다. 나는 그것을 어떻게 할 수 있습니까?


사실 나는 이미 레이아웃 country_row.xml을 적용하고 커서 어댑터에서이

boolean i = prefs.getBoolean("fontBold", false); 
System.out.println(i); 
if (i){ 
     TextView tv = (TextView)findViewById(R.id.year); 
     tv.setTypeface(null, Typeface.BOLD);//null pointer 
} 

같은 공유 환경 설정에서 값을 얻을 수 있습니다. 그렇다면이 레이아웃에 들어가는 환경 설정 값을 어떻게 사용할 수 있습니까? 체크 박스에서 가져 오는 불리언 값은 올바른 것으로 표시되어 인쇄됩니다. 그러나 위와 같이하려고하면 프로그램이 작동하지 않고 null 포인터 예외를 말하는 프로그램이 중단됩니다.

여기에 붙어있는 것은 .... 올바른 환경 설정 값을 얻지 만이를 사용하거나 기존 레이아웃에 적용하는 방법을 모르거나 다른 레이아웃을 만들어야합니다. .. 나는 그것에 관해 확실하지 않다.

답변

1

코드에서 동적으로 일부 레이아웃 매개 변수를 변경하는 것으로 생각합니다. 일반적으로 코드 또는 .xml 파일을 통해보기 (레이아웃, 텍스트보기, 편집 텍스트 등)에 속성을 설정할 수 있습니다. 예를 들어 레이아웃을 선택하십시오. 먼저 레이아웃에 id 속성을 추가하십시오.

<LinearLayoout android:id="@+id/layoutID" .../> 

그런 다음

LinearLayout layout=(LinearLayout)findViewById(R.id.layoutID) //get the layout object. 
layout.setBackgroundColor (color from your preferences); 

위의 기본적인 생각이다. mor 정보를 찾으려면 SDK 문서를 읽으십시오.

+0

시도했지만 작동하지 않았습니다. – SASM

+0

무엇이 문제입니까? 색상을 저장하려면 int 변수를 사용하는 것이 좋습니다. – Huang

+0

내가 공유 환경 설정에서 가져 오는 값은 내가 마지막으로 (on/off) 왼쪽으로 체크 박스를 볼 때 계속 유지됩니다. 또한 getBoolean 또는 getInt..but 값을 얻을 수 있습니다. 위에서 언급 한대로 country_row xml 레이아웃을 가지고 있습니다. 현재 내 질문에 apply.i도 findViewById (R.id.layoutID)를 얻는 방법을 알고 있습니다. 문제는 내가 가지고있는 환경 설정 값으로 font color 또는 bg color를 변경하는 방법이다. 그래서 country_row xml은이 변경된 레이아웃을 표시 할 것이다. – SASM

0

Huang 대답처럼 1) 속성을 변경해야하는 레이아웃 또는보기에 대한 참조를 만들어야합니다. 2) 그런 다음 환경 설정 값을 해당 변수로 가져옵니다. 3) 값을 설정하여 해당 레이아웃 또는 뷰를 적용합니다.

그것은 효과가 없을 것입니다. 자세한 내용은 아래 확인란을 선택하여 음악을 연주하는 것과 같은 동작을 수행하는 예제를 참조하십시오.

myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); 
    fullResultSound = myPrefs.getBoolean("FullResultIsOn", false); 
    lessResultSound = myPrefs.getBoolean("LessResultIsOn", false); 

    System.out.println("============================= The FullResultSound in Result Page is: "+fullResultSound); 
    System.out.println("============================= The LessResultSound in Result Page is: "+lessResultSound); 


if(fullResultSound) 
     { 
      playSound(soundFileForFullResult); 
     } 
     else 
     { 
      playSound(); 
     } 

희망은 도움이 될 것입니다. 그렇지 않다면 말해주세요.

0

실제로보기의 레이아웃을 어떻게 변경했는지는이 작업을 수행하는 것이 었습니다. ContentResolver contentResolver = getContentResolver();

Cursor c = contentResolver.query(CONTENT_URI, null, null, null, sortOrder);  
    String[] from = new String[] { "year", "country" }; 
    int[] to = new int[] { R.id.year, R.id.country };  
    SimpleCursorAdapter sca = new MySimpleCursorAdapter(this, R.layout.country_row, 
      c, from, to); 
    setListAdapter(sca); 

class MySimpleCursorAdapter extends SimpleCursorAdapter{ 

    public MySimpleCursorAdapter(Context context, int layout, Cursor c, 
      String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     // TODO Auto-generated constructor stub 
    } 


    @Override // Called when updating the ListView 
    public View getView(int position, View convertView, ViewGroup parent) { 
     /* Reuse super handling ==> A TextView from R.layout.list_item */ 
     View v = super.getView(position,convertView,parent); 

     TextView tYear = (TextView) v.findViewById(R.id.year); 
     TextView tCountry = (TextView) v.findViewById(R.id.country); 

     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
     boolean font_size = prefs.getBoolean("fontSize", false); 
     boolean italic_font = prefs.getBoolean("fontItalic", false); 


     String listpref = prefs.getString("bgColor", "#ffffff80"); 
     //System.out.println(listpref); 
     tYear.setBackgroundColor(Color.parseColor(listpref)); 
     tCountry.setBackgroundColor(Color.parseColor(listpref)); 


     if (font_size){ 
      tYear.setTextSize(25); 
      tCountry.setTextSize(25); 
     } 


     if (italic_font){ 
      tYear.setTypeface(null, Typeface.ITALIC); 
      tCountry.setTypeface(null, Typeface.ITALIC); 
     } 

     //tv.setBackgroundColor(col); 

     return v;  
    } 
} 
관련 문제