三、自定义控件
①title.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="wrap_content"
android:background="@drawable/bg_bottombar_center_normal">
<Button
android:id="@+id/title_bcak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/bg_chat_dis_normal"
android:text="Back"
android:textColor="#fff"
/>
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#fff"
android:textSize="24sp"
/>
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/bg_chat_active"
android:text="Edit"
android:textColor="#fff"
/>
</LinearLayout>
②main_layout.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">
<com.example.ewewwfef.anewtest.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.ewewwfef.anewtest.TitleLayout>
</LinearLayout>
③TitleLayout.java中的代码
package com.example.ewewwfef.anewtest;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
* Created by ewewwfef on 2018/3/18.
*/
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs)
{
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title,this);
Button titleBack=(Button)findViewById(R.id.title_bcak);
Button titleEdit=(Button)findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
((Activity)getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),"You clicked Edit button",Toast.LENGTH_SHORT).show();
}
});
}
}
④主活动中的代码
package com.example.ewewwfef.anewtest;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ActionBar actionBar=getSupportActionBar();
if(actionBar!=null)
actionBar.hide();
}
}
四、ListView
Main_layout中的代码
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
主活动中的代码
public class MainActivity extends AppCompatActivity {
private String[]data={"Apple","Banana","Orange","Watermelon",
"Pear","Grape","Pineapple","Strawberry","cherry","Mango",
"Apple","Banana","Orange","Watermelon", "Pear","Grape",
"Pineapple","Strawberry","cherry","Mango"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(
MainActivity.this,android.R.layout.simple_list_item_1,data
);
ListView listView=(ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
在主程序中,使用了ArrayAdapter作为适配器,来储存data中的数据。 最后需要调用setAdapter,将适配的数据传入
五、定制listview界面,以使其显得更丰富
①Fruit类中的代码
package com.example.ewewwfef.anewtest;
/**
* Created by ewewwfef on 2018/3/18.
*/
public class Fruit {
private String name;
private int inmageId;
public Fruit(String name,int imageId)
{
this.name=name;
this.inmageId=imageId;
}
public String getName()
{
return name;
}
public int getInmageId()
{
return inmageId;
}
}
②fruit_item.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">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
③main_layout.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">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
④FruitAdapter.java中的代码
package com.example.ewewwfef.anewtest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by ewewwfef on 2018/3/18.
*/
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId,
List<Fruit> objects)
{
super(context,textViewResourceId,objects);
resourceId=textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Fruit fruit=getItem(position);
View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,
false);
ImageView fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
TextView fruitName=(TextView)view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getInmageId());
fruitName.setText(fruit.getName());
return view;
}
}
⑤MainActivity.java中的代码
package com.example.ewewwfef.anewtest;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
initFruit();
FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.fruit_item,
fruitList);
ListView listView=(ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruit()
{
for(int i=0;i<2;i++)
{
Fruit apple=new Fruit("Apple",R.drawable.apple_img);
fruitList.add(apple);
Fruit banana=new Fruit("Bnanana",R.drawable.banana_img);
fruitList.add(banana);
Fruit orange=new Fruit("Orange",R.drawable.orange_img);
fruitList.add(orange);
Fruit watermelon=new Fruit("Watermelon",R.drawable.watermelon_img);
fruitList.add(watermelon);
Fruit pear=new Fruit("Pear",R.drawable.pear_img);
fruitList.add(pear);
Fruit grape=new Fruit("Grape",R.drawable.grape_img);
fruitList.add(grape);
Fruit pineapple=new Fruit("Pineapple",R.drawable.pineapple_img);
fruitList.add(pineapple);
Fruit strawberry=new Fruit("Strawberry",R.drawable.strawberry_img);
fruitList.add(strawberry);
Fruit cherry=new Fruit("Cherry",R.drawable.cherry_img);
fruitList.add(cherry);
Fruit mongo=new Fruit("Mongo",R.drawable.mongo_img);
fruitList.add(mongo);
}
}
}