我有這樣的XML:我不能居中文本在我的列表視圖
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AllJokes"
android:background="@drawable/background">
<ListView
android:id="@+id/allJokesList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
</ListView>
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
ads:adUnitId="ca-app-pub-code/code"
ads:adSize="BANNER"
/>
</LinearLayout>
正如你可以看到我嘗試使用android:gravity="center_vertical"
居中listView
的文本,但沒有成功。我也試圖通過:android:gravity="center"
,但它總是左對齊。
而且在Java代碼中我設置由白色背景的listView
:
listView.setBackgroundColor(Color.WHITE);
是它搞亂的東西嗎?
這是怎麼發生的?
我知道我錯過了一些非常小而基本的東西,但作爲初學者,我無法發現它。
編輯:
這裏是我使用的項目添加到列表視圖代碼:
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
adapter.clear();
adapter.addAll(globals.getMyStringArray());
listView = (ListView) findViewById(R.id.allJokesList);
listView.setAdapter(adapter);
listView.setBackgroundColor(Color.WHITE);
凡globals.getMyStringArray()
已填充了值。
後您的列表項xml – Libin
@Libin以上xml是我用於該活動的唯一一個。 – chility
如何添加項目到列表視圖? – Libin