首先新增Android操作。耐心一點。這就是說,它正在做我的頭。如何定製你的ListView的6個不同的例子,到目前爲止沒有任何工作。顯然是我。這應該是如此簡單,我必須錯過一個重要的步驟。我看過無數的視頻和閱讀教程似乎都聲稱它能夠正常工作。Cutsomise ListView Android中的文本
所以這是發生了什麼事:
1)我有一個活動。
2)這有一個說哺乳動物的StringArray。
3)我用水果數組填充ListView。
4)除了有黑色背景,我想要一個帶有黑色文本的白色背景。
5)相關的圖像添加到動物,即兔子有兔子的縮略圖(請注意我甚至沒有得到這一步)
所以這是我回顧其他國家的人民建議後做;
1)所生成的左稱爲"mammals.xml"
這有ListView控件在它的可視頁面上我原來的ListView項調用自定義佈局"list_row.xml"
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/logo"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
>
</ImageView>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30px" >
</TextView>
</LinearLayout>
2)。
<ListView android:id="@+id/list"
android:layout_height="300dp"
android:layout_width="300dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="114dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:background="#FFFFFF"/>
3)在"onCreate()"
我添加了列表並應用它。
static final String[] MammalList = new String[] { "Rabbit", "Bear", "Deer","Kangaroo", "Emu", "Leopard" };
private ListView m_listview;
@Override
protected void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mammals);
m_listview = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_row, MammalList);
m_listview.setAdapter(adapter);
我知道,爲了改變怎麼行是看你改變android.R.layout.simple_list_item_1
到R.layout.list_row
。此時我運行該應用程序並失敗。
有人可以請指出我如何實現這一目標的正確方向嗎?
也不確定Stack Overflow是否捕獲了所有的代碼。我嘗試了兩種不同的瀏覽器,這兩種瀏覽器都有渲染問題。
錯誤下
04-02 17:53:37.621: ERROR/AndroidRuntime(30020): FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2159)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
at android.widget.ListView.onMeasure(ListView.java:1158)
at android.view.View.measure(View.java:15518)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
at android.view.View.measure(View.java:15518)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
'在這一點上我運行應用程序,它fails.'你什麼錯誤?你能複製粘貼錯誤LogCat嗎? – Nezam
嘗試改變你的主題android.holo,我想你會得到與黑色文本的白色背景 –
嘗試更改您的適配器,像這樣... setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,MammalList)); –