在 Android 中自訂 ListView 行項目
目前的任務涉及建立一個 ListView,其中的行顯示標題,然後更改文字。為此,請按照下列步驟操作:
行項目的自訂佈局:
<code class="xml"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Header"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text"/> </LinearLayout></code>
主XML 版面:
<code class="xml"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent"> </ListView> </LinearLayout></code>
自訂適配器類別:
<code class="java">class yourAdapter extends BaseAdapter { Context context; String[] data; private static LayoutInflater inflater = null; public yourAdapter(Context context, String[] data) { this.context = context; this.data = data; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } // ... Implement other methods as required by BaseAdapter // such as getView(), getCount(), getItem(), getItemId() }</code>
Java Activity:
<code class="java">public class StackActivity extends Activity { ListView listview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listview = (ListView) findViewById(R.id.listview); listview.setAdapter(new yourAdapter(this, new String[] { "data1", "data2" })); } }</code>
以上是如何在 Android 中建立具有自訂行項目和動態變更文字的 ListView?的詳細內容。更多資訊請關注PHP中文網其他相關文章!