目录
概要
ScrollView控件只允许包含一个子节点,通常放入LinearLayout控件,根据是需要水平滚动或垂直滚动设置LinearLayout的orientation属性为horizontal或vertical
- <ScrollView 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="cn.teachcourse.main.MainActivity" >
- <LinearLayout
- android:id="@+id/scroll_view_ll"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- </LinearLayout>
- </ScrollView>
news_list_item布局
- <?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="wrap_content" >
- <ImageView
- android:id="@+id/news_list_profile_image_iv"
- style="@style/weixin_news_list_img_c"
- android:layout_marginRight="@dimen/shadow_width"
- android:contentDescription="@string/news_each_info"
- android:src="@drawable/ic_launcher"
- android:scaleType="fitXY" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:layout_marginLeft="@dimen/shadow_width"
- android:layout_toLeftOf="@+id/news_list_profile_image_iv"
- android:orientation="vertical"
- android:id="@+id/news_list_item_ll" >
- <TextView
- android:id="@+id/news_title_tv"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/rec_tone"
- android:textSize="@dimen/list_cat_size"
- android:textColor="@color/text_black_title" />
- <TextView
- android:id="@+id/news_overview_tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/news_overview_info"
- android:textColor="@color/text_gray_title"
- android:textSize="@dimen/list_dis_tv_size" />
- </LinearLayout>
- <View
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:background="@color/list_item_bg_selector"
- android:layout_below="@+id/news_list_item_ll"
- android:layout_marginTop="4dp"/>
- </RelativeLayout>
news_list_item_more_img布局
- <?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="120dp" >
- <RelativeLayout
- android:id="@+id/news_more_img_rl"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@+id/news_list_item_text_iv">
- <LinearLayout
- android:id="@+id/news_horizontal_ll"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/news_list_item_img_one_iv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="5dp"
- android:layout_weight="1"
- android:src="@drawable/ic_launcher" />
- <ImageView
- android:id="@+id/news_list_item_img_two_iv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="5dp"
- android:layout_weight="1"
- android:src="@drawable/ic_launcher" />
- <ImageView
- android:id="@+id/news_list_item_img_three_iv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="5dp"
- android:layout_weight="1"
- android:src="@drawable/ic_launcher" />
- </LinearLayout>
- </RelativeLayout>
- <TextView
- android:id="@+id/news_list_item_text_iv"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="@string/display_news"
- android:textColor="@color/text_black_title"
- android:textSize="@dimen/list_cat_size" />
- </RelativeLayout>
创建新闻JavaBean实体NewsBean
每一个NewsBean实体对应一条新闻内容,实体包含属性值如下:
- /*
- @author TeachCourse博客分享(https://www.teachcourse.cn)
- @date 创建于:2015-10-26
- */
- public class NewsBean implements Serializable {
- private String id;// 新闻编号
- private String title; // 新闻标题
- private String content; // 新闻摘要内容
- private String full_title; // 完整标题
- private String pdate; // 发布时间
- private String src; // 新闻来源
- private String img_width; // 图片宽度
- private String img_length;// 图片高度
- private String img; // 图片链接
- private String url; // 新闻链接
- private String pdate_src;// 发布完整时间
- }
动态添加news_list_item.xml、news_list_item_more_img.xml到LinearLayout
- private void showView() {
- for (int i = 0; i < mList.size(); i++) {
- int type = i % 4;
- // 初始化news_list_item布局
- View news_list_item_view = LayoutInflater.from(this).inflate(
- R.layout.news_list_item, mLinearLayout, false);
- // 初始化news_list_item__more_img_view布局
- View news_list_item__more_img_view = LayoutInflater.from(this)
- .inflate(R.layout.news_list_item_more_img, mLinearLayout,
- false);
- switch (type) {
- case 0:
- TextView mTitle_More_TV = (TextView) news_list_item__more_img_view
- .findViewById(R.id.news_list_item_text_iv);
- mTitle_More_TV.setText(mList.get(i).getTitle());
- mLinearLayout.addView(news_list_item__more_img_view);
- break;
- default:
- TextView mTitle_TV = (TextView) news_list_item_view
- .findViewById(R.id.news_title_tv);
- TextView mContent_TV = (TextView) news_list_item_view
- .findViewById(R.id.news_overview_tv);
- mTitle_TV.setText(mList.get(i).getTitle());
- mContent_TV.setText(mList.get(i).getContent());
- mLinearLayout.addView(news_list_item_view);
- break;
- }
- }
- }
》运行效果图
你可能感兴趣的文章
来源:TeachCourse,
每周一次,深入学习Android教程,关注(QQ158#9359$239或公众号TeachCourse)
转载请注明出处: https://www.teachcourse.cn/1180.html ,谢谢支持!
转载请注明出处: https://www.teachcourse.cn/1180.html ,谢谢支持!
分类:Android
标签:Android, ScrollView