studentPDY

第六次作业

package com.example.myapplication;

import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    private String[] titles = { "京东商城", "qq", "qq斗地主", "新浪微博", "天猫", "uc浏览器" ,"微信"};
    private int[] pic = { R.drawable.jd,
            R.drawable.qq,
            R.drawable.ddz,
            R.drawable.wb,
            R.drawable.tm,
            R.drawable.llq,
            R.drawable.wx};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取Listview
        ListView lv = (ListView) findViewById(R.id.listView1);

        // 创建adapter对象
        Myadapter md = new Myadapter();

        lv.setAdapter(md);
    }
    class Myadapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO 自动生成的方法存根
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO 自动生成的方法存根
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO 自动生成的方法存根
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO 自动生成的方法存根
            //加载列表项的布局文件
            View view=View.inflate(MainActivity.this, R.layout.list_item, null);
            TextView tv1=(TextView)view.findViewById(R.id.name);

            ImageView iv=(ImageView)view.findViewById(R.id.iv);

            tv1.setText(titles[position]);

            iv.setBackgroundResource(pic[position]);

            return view;
        }

    }

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapplication.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerVertical="true"/>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/iv"
        android:layout_centerVertical="true">
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40sp"
            android:textColor="#000000" />


    </RelativeLayout> </RelativeLayout>


 

posted on 2019-10-09 13:20  studentPDY  阅读(...)  评论(...编辑  收藏

My Links

Blog Stats