2011-09-16 45 views
3

バックグラウンドページ全体を白くするにはどうすればよいですか? 私はリストビューを持っていて、xmlに背景色を設定しようとしましたが、 は機能しませんでした。リストビューで背景色を設定する

public class Contactenlijst extends ListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    final HashMap<Integer, Personeel> personeelmap = new HashMap<Integer, Personeel>(); 
    ArrayList<String> list = new ArrayList<String>(); 
    // Get the data (see above) 

    JSONObject json = Database 
      .getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php"); 

    try { 
     JSONArray contactinfo = json.getJSONArray("contactlijst"); 
     // Loop the Array 
     for (int i = 0; i < contactinfo.length(); i++) { 

      JSONObject e = contactinfo.getJSONObject(i); 
      Personeel p = new Personeel(
        Integer.parseInt(e.getString("id")), 
        e.getString("staff_name"), 
        e.getString("staff_lastname"), 
        e.getString("staff_dateofbirth"), 
        e.getString("staff_address"), 
        e.getString("staff_address_postal"), 
        e.getString("staff_address_city"), 
        e.getString("staff_mobile")); 
      personeelmap.put(Integer.parseInt(e.getString("id")), p); 
      list.add(p.toString()); 
     } 

    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data " + e.toString()); 
    } 

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list)); 
    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    // onclick stuur array naar contactinfo 

    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      String text = ((TextView) view).getText().toString(); 
      Intent i = new Intent(Contactenlijst.this, Contactinfo.class); 
      String uittekst[] = text.split(" "); 
      String vnaam = uittekst[0].toString(); 

      ArrayList<String> al = new ArrayList<String>(); 
      int a = personeelmap.size(); 

      a = a + 1; 
      for (int c = 1; c < a; c++) { 

       if (personeelmap.get(c).getStaff_name().toString() 
         .equals(vnaam)) { 
        al.add(personeelmap.get(c).getStaff_name()); 
        al.add(personeelmap.get(c).getStaff_lastname()); 
        al.add(personeelmap.get(c).getDateofbirth()); 
        al.add(personeelmap.get(c).getStaff_address()); 
        al.add(personeelmap.get(c).getStaff_address_postal()); 
        al.add(personeelmap.get(c).getStaff_address_city()); 
        al.add(personeelmap.get(c).getStaff_mobile()); 
       } 
       ; 
      } 

      i.putStringArrayListExtra("array", al); 
      startActivity(i); 
     } 
    }); 
} 

}

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/naam" android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="10dp" 
android:textSize="16sp" > 
</TextView> 

が、これは私のJavaコードです:

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

<ListView 
android:id="@+id/list" android:layout_width="fill_parent" 
android:clickable="true" android:layout_height="fill_parent"></ListView> 

実際に白い取得する唯一のものはこれです: これらは私のXMLSです

+1

でこの色を呼んでいるのですか?スクリーンショットを投稿できますか? – Moss

答えて

13

非常に簡単な方法があります。

まず、(のonCreate上)あなたの活動にListViewコントロール変数を作成します。

ListView LV = null; 

その後のonCreateであなたがListActivityを拡張すると仮定すると、あなたがこれを行うことができ、変数を設定するには:

LV = getListView(); 

かこれはリストビューに対してXMLでmylist idが設定されていると仮定して動作します。

LV = (ListView) findViewById(R.id.mylist); 

あなたは、インデックス「0」で背景色を設定したい場合は、Uはこのように動作しようとしなければならない

int r = getResources().getIdentifier("subtle_white_gradient", "drawable", "com.my.package"); 
LV.setBackgroundResource(r); 
+0

プログラムで設定できますが、レイアウトはXMLで行います。android:background = "@ color/background" does not work。なぜこのような振る舞いですか? – AKh

+0

@color/whiteのようなものは、あらかじめ定義された色で動作するか、strings.xmlで独自に作成できます。http://stackoverflow.com/questions/2748830/how-to-change-background-color-in- android-app – Mischa

+0

@Mischa res/values/colors.xmlの意味ですか? – Piovezan

-1

は、XMLでこれを試してみてください:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <ListView android:id="@+id/list" 
      android:layout_width="fill_parent" 
      android:clickable="true" 
      android:layout_height="fill_parent" 
      android:background="@color/background" > 
     </ListView> 

そして、あなたが望む色を定義colors.xmlと呼ばれるXMLファイルを作成します。

<color name="background">#CDB38B </string> 
+0

それはうまくいかないのですか?それが私に知らせてくれたら... – sanjay

+1

@color属性は/values/colors.xmlに定義する必要があります – AKh

1

を最も簡単な方法は、set a themeにあります。 manifest.xmlファイルで、この属性をアクティビティに追加してください:

android:theme="@android:style/Theme.Light" 
5

LV.setBackgroundColor(Color.WHITE); 

や描画可能に設定します。最後に、色に背景を設定いいです、私はこのコードを使いました。

list.post(new Runnable() { 

    @Override 
    public void run() { 
     list.setSelected(true); 
     list.setBackgroundColor(Color.BLACK); 
     list.getChildAt(0).setBackgroundColor(Color.BLUE);   
    } 
}); 
2

あなたはあなたのプロジェクトのres/valueフォルダにcolor.xmlファイルを作成する必要があります。 color.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="orange">#ff5500</color> 
<color name="white">#ffffff</color> 
<color name="transparent">#00000000</color> 
<color name="black">#000000</color> 
<color name="gray">#999999</color> 
<color name="blue">#0066cc</color> 

</resources> 

が今まさに今が起こるんどのようなあなたのLisiview

<ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@color/blue"/> 
関連する問題