AIDE Android App基础教程答案_第十五课:Custom ListView(自定义列表视图)

第十五课,本课中您将开始学习自定义列表视图 运行这个程序,显示公司名称的列表

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        	"Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
        	
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
}

我们通过XML布局文件来添加自定义列表条目,添加“TextView”的“android:layout_width=”wrap_content””、“android:layout_height=”wrap_content”

<?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="vertical">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>

</LinearLayout>

设置属性“android:id”为“@+id/entryTextView1”,分配ID给“TextView

<?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="vertical">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:id="@+id/entryTextView1"/>

</LinearLayout>

条目布局适配器“ListAdapter”通过参数“(this, R.layout.entry, R.id.entryTextView1, companies)”组成构造函数,“R.layout.entry”使适配器使用条目布局、使用“R.id.entryTextView1”标识文本视图、文本视图使用companies数组的文本填充

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        	"Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
        	
        ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.entry,R.id.entryTextView1,companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
}

设置“TextView”的“android:padding”为“10dp”、“android:textSize”为25spandroid:textStylebold

<?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="vertical">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:id="@+id/entryTextView1"
		android:textSize="25sp"
		android:padding="10dp"
		android:textStyle="bold"/>

</LinearLayout>

欢迎加入AIDE教程网官方交流群:758110864

 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/1932.html

梦雪

专注于AIDE教程分享

1 Comment

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐