简介:经典的红色电影是指在 1942 年毛泽东发表了《在延安文艺座谈会上的讲话》之后,产生的大量反映时代、对人民群众有重要影响的一批小说、戏剧、电影等题材作品。 和以往板起面孔的人物形象相对比,现在的红色电影更有人情味,人物也更显有血有肉。请同学们,找出自己看过的印象最深刻的 5 部电影
1.搭建 Android studio 开发环境;
2. 界面设计,写布局文件源码;安置图片、文字等资源。
①布局文件有两个:
(1)activity_main.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" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:gravity="center_horizontal" android:paddingTop="5dp" android:layout_height="40dp" android:layout_marginBottom="30dp" android:text="红色革命电影" android:textSize="20sp" android:textColor="@color/white" android:background="@color/orange" /> <ListView android:id="@+id/listview" android:layout_marginLeft="15dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
(2) linearlayout.xml文件代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ImageView android:id="@+id/text1" android:layout_width="80dp" android:layout_height="130dp" android:scaleType="centerInside" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginLeft="15dp" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="@color/black" /> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingLeft="5dp" android:textSize="15sp" android:textColor="@color/orange" /> </LinearLayout> </LinearLayout>
②安置图片资源
(1)放进drawable目录下
直接复制图片,粘贴进去就行,图片命名不能以数字开头!
3. 源码设计,写源文件源码;
MainActivity文件代码如下:
package com.edu.nchu.lab2exam03; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //根据id找出ListView ListView listView = (ListView) findViewById(R.id.listview); //创建SimpleAdapter时需要传一个List<Map<Object,Object>>作为参数 List<Map<String,Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); //放进ImageView的资源 map1.put("icon", R.drawable.one); //放进TextView的内容 map1.put("msg1", "《闪闪的红星》"); map1.put("msg2", "主演:祝新运/赵汝平"); list.add(map1); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("icon", R.drawable.two); map2.put("msg1", "《八佰》"); map2.put("msg2", "主演:黄志忠/欧豪/王千源"); list.add(map2); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("icon", R.drawable.three); map3.put("msg1", "《建军大业》"); map3.put("msg2", "主演:刘烨/冯远征/张嘉益"); list.add(map3); Map<String, Object> map4 = new HashMap<String, Object>(); map4.put("icon", R.drawable.four); map4.put("msg1", "《地道战》"); map4.put("msg2", "主演:任旭东/徐国腾"); list.add(map4); Map<String, Object> map5 = new HashMap<String, Object>(); map5.put("icon", R.drawable.five); map5.put("msg1", "《鸡毛信》"); map5.put("msg2", "主演:蔡元元/舒适"); list.add(map5); //将资源和对应的布局控件显示出来 String[] from = {"icon","msg1","msg2"}; int[] to = {R.id.text1, R.id.textview1,R.id.textview2}; //new一个SimpleAdapter,将参数传进去 SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, list , R.layout.linearlayout,from ,to); //将SimpleAdapter设置进ListView中 listView.setAdapter(simpleAdapter); //下面绑定监听器,通过Toast弹出被点击文本的内容 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(view instanceof LinearLayout){ //获得一个view,实际上是一个LinearLayout View LinearLayout linearLayout1 = (LinearLayout) view; //LinearLayout的第二个子View也是一个LinearLayout View,是两个TextView View viewSub = linearLayout1.getChildAt(1); if(viewSub instanceof LinearLayout){ LinearLayout linearLayout2 = (LinearLayout) viewSub; //获得两个TextView的内容,并通过Toast弹出来 TextView tv1 = (TextView) linearLayout2.getChildAt(0); TextView tv2 = (TextView) linearLayout2.getChildAt(1); String msg = tv1.getText().toString() + "\n" + tv2.getText().toString(); Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show(); } } } }); }}
4. 在模拟器运行;
5. 记录错误信息和改正;
编码过程中注意布局的影响!
有任何问题欢迎评论或者私信,看见就回☺