第六次作业
1.1.1
<?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"
tools:context=".MainActivity">
<?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"
tools:context=".MainActivity">
<ListView
android:id="@+id/lView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:id="@+id/lView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
1.1.2
<?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="horizontal">
<LinearLayout
android:layout_margin="18dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_margin="18dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/yhi"
android:layout_width="60dp"
android:layout_height="60dp" />
android:id="@+id/yhi"
android:layout_width="60dp"
android:layout_height="60dp" />
<TextView
android:id="@+id/Tb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textSize="35sp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
android:id="@+id/Tb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textSize="35sp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
1.1.1
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private String arr[]={"京东商城","QQ斗地主","QQ","UC浏览器","天猫","新浪微博","微信"};
private int icons[] = new int[]{R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4, R.drawable.b5, R.drawable.b6, R.drawable.b7};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.lView);
private ListView listView;
private String arr[]={"京东商城","QQ斗地主","QQ","UC浏览器","天猫","新浪微博","微信"};
private int icons[] = new int[]{R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4, R.drawable.b5, R.drawable.b6, R.drawable.b7};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.lView);
MyBaseAdapter myBaseAdapter=new MyBaseAdapter();
listView.setAdapter(myBaseAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"你点击的是第"+(position+1)+"个条目", Toast.LENGTH_SHORT).show();
}
});
}
protected class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
return arr.length;
}
protected class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
return arr.length;
}
@Override
public Object getItem(int position) {
return arr[position];
}
public Object getItem(int position) {
return arr[position];
}
@Override
public long getItemId(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=View.inflate(MainActivity.this,R.layout.tyihui,null);
ImageView imageView=view.findViewById(R.id.yhi);
TextView textView=view.findViewById(R.id.Tb);
imageView.setBackgroundResource(icons[position]);
textView.setText(arr[position]);
return view;
} }
public View getView(int position, View convertView, ViewGroup parent) {
View view=View.inflate(MainActivity.this,R.layout.tyihui,null);
ImageView imageView=view.findViewById(R.id.yhi);
TextView textView=view.findViewById(R.id.Tb);
imageView.setBackgroundResource(icons[position]);
textView.setText(arr[position]);
return view;
} }
}