Android开发初体验

Posted wuwuFQ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发初体验相关的知识,希望对你有一定的参考价值。

学习地址:大佬的博客放在前面,以示尊敬:

先贴两张经常用的布局

  1. 线性布局
  2. 相对布局

根据学习自己尝试的静态页,效果图(就一张)

项目结构

一共五个文件,贴一下代码:

  • MainActivity
package com.example.myapplication;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import java.util.LinkedList;
import java.util.List;

public class MainActivity extends Activity {
    private List<Animal> mData = null;
    private Context mContext;
    private AnimalAdapter mAdapter = null;
    private ListView list_animal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = MainActivity.this;
        list_animal = (ListView) findViewById(R.id.list_animal);
        mData = new LinkedList<Animal>();
        mData.add(new Animal("旺旺", "你是狗么?", "小狗"));
        mData.add(new Animal("槑槑", "你是牛么?", "小牛犊"));
        mData.add(new Animal("嘎嘎", "你是鸭么?", "小鸭子"));
        mData.add(new Animal("尼莫", "你是鱼么?", "鱼儿"));
        mData.add(new Animal("佩德罗", "你是马么?", "马儿"));
        mAdapter = new AnimalAdapter((LinkedList<Animal>) mData, mContext);
        list_animal.setAdapter(mAdapter);
    }
}
  • Animal
package com.example.myapplication;

public class Animal {
    private String name;
    private String speak;
    private String category;

    public Animal() {
    }

    public Animal(String name, String speak, String category) {
        this.name = name;
        this.speak = speak;
        this.category = category;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSpeak() {
        return speak;
    }

    public void setSpeak(String speak) {
        this.speak = speak;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
}

  • AnimalAdapter
package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class AnimalAdapter extends BaseAdapter {
    private List<Animal> dataArr;
    private Context context;

    public AnimalAdapter(List<Animal> dataArr, Context context) {
        this.dataArr = dataArr;
        this.context = context;
    }

    @Override
    public int getCount() {
        return dataArr.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item_list_animal,parent,false);
        TextView txt_aCategory = (TextView) convertView.findViewById(R.id.txt_aCategory);
        TextView txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);
        TextView txt_aSpeak = (TextView) convertView.findViewById(R.id.txt_aSpeak);
        txt_aCategory.setText(dataArr.get(position).getCategory());
        txt_aName.setText(dataArr.get(position).getName());
        txt_aSpeak.setText(dataArr.get(position).getSpeak());
        return convertView;
    }
}
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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:layout_gravity="center"
    android:gravity="center"
    tools:context=".MainActivity">
    <ListView
        android:id="@+id/list_animal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>
  • item_list_animal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:gravity="center"
        android:id="@+id/txt_aCategory"
        android:layout_width="50dp"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:padding="10dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/txt_aName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/txt_aSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

以上是关于Android开发初体验的主要内容,如果未能解决你的问题,请参考以下文章

Android开发学习之路--RxAndroid之初体验

Android开发初体验

Android开发初体验

Android开发学习之路--Notification之初体验

第一章 Android初体验

Android开发学习之路--Annotation注解简化view控件之初体验