Android开发之ScrollView控件Demo演示

2015-12-31 14:48 评论 0 条

概要

ScrollView控件只允许包含一个子节点,通常放入LinearLayout控件,根据是需要水平滚动或垂直滚动设置LinearLayout的orientation属性为horizontal或vertical

》Demo效果
demo

  1. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="cn.teachcourse.main.MainActivity" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/scroll_view_ll"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:orientation="vertical" >  
  12.     </LinearLayout>  
  13.   
  14. </ScrollView>  

news_list_item布局

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/news_list_profile_image_iv"  
  8.         style="@style/weixin_news_list_img_c"  
  9.         android:layout_marginRight="@dimen/shadow_width"  
  10.         android:contentDescription="@string/news_each_info"  
  11.         android:src="@drawable/ic_launcher"  
  12.         android:scaleType="fitXY" />  
  13.   
  14.     <LinearLayout  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_centerInParent="true"  
  18.         android:layout_marginLeft="@dimen/shadow_width"  
  19.         android:layout_toLeftOf="@+id/news_list_profile_image_iv"  
  20.         android:orientation="vertical"  
  21.         android:id="@+id/news_list_item_ll" >  
  22.   
  23.         <TextView  
  24.             android:id="@+id/news_title_tv"  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="@string/rec_tone"  
  28.             android:textSize="@dimen/list_cat_size"  
  29.             android:textColor="@color/text_black_title" />  
  30.   
  31.         <TextView  
  32.             android:id="@+id/news_overview_tv"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:text="@string/news_overview_info"  
  36.             android:textColor="@color/text_gray_title"  
  37.             android:textSize="@dimen/list_dis_tv_size" />  
  38.     </LinearLayout>  
  39.     <View   
  40.         android:layout_width="match_parent"  
  41.         android:layout_height="0dp"  
  42.         android:background="@color/list_item_bg_selector"  
  43.         android:layout_below="@+id/news_list_item_ll"  
  44.         android:layout_marginTop="4dp"/>  
  45.       
  46. </RelativeLayout>  

news_list_item_more_img布局

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="120dp" >  
  5. <RelativeLayout   
  6.     android:id="@+id/news_more_img_rl"  
  7.     android:layout_width="match_parent"  
  8.     android:layout_height="match_parent"  
  9.     android:layout_below="@+id/news_list_item_text_iv">  
  10.     <LinearLayout  
  11.         android:id="@+id/news_horizontal_ll"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:orientation="horizontal" >  
  15.   
  16.         <ImageView  
  17.             android:id="@+id/news_list_item_img_one_iv"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_margin="5dp"  
  21.             android:layout_weight="1"  
  22.             android:src="@drawable/ic_launcher" />  
  23.   
  24.         <ImageView  
  25.             android:id="@+id/news_list_item_img_two_iv"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_margin="5dp"  
  29.             android:layout_weight="1"  
  30.             android:src="@drawable/ic_launcher" />  
  31.   
  32.         <ImageView  
  33.             android:id="@+id/news_list_item_img_three_iv"  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:layout_margin="5dp"  
  37.             android:layout_weight="1"  
  38.             android:src="@drawable/ic_launcher" />  
  39.     </LinearLayout>  
  40. </RelativeLayout>  
  41.     <TextView  
  42.         android:id="@+id/news_list_item_text_iv"  
  43.         android:layout_width="match_parent"  
  44.         android:layout_height="wrap_content"  
  45.         android:layout_alignParentLeft="true"  
  46.         android:layout_alignParentTop="true"  
  47.         android:text="@string/display_news"  
  48.         android:textColor="@color/text_black_title"  
  49.         android:textSize="@dimen/list_cat_size" />  
  50.   
  51. </RelativeLayout>  

创建新闻JavaBean实体NewsBean

每一个NewsBean实体对应一条新闻内容,实体包含属性值如下:

  1. /* 
  2.  @author TeachCourse博客分享(https://www.teachcourse.cn) 
  3.  @date 创建于:2015-10-26 
  4.  */  
  5. public class NewsBean implements Serializable {  
  6.     private String id;// 新闻编号  
  7.     private String title; // 新闻标题  
  8.     private String content; // 新闻摘要内容  
  9.     private String full_title; // 完整标题  
  10.     private String pdate; // 发布时间  
  11.     private String src; // 新闻来源  
  12.     private String img_width; // 图片宽度  
  13.     private String img_length;// 图片高度  
  14.     private String img; // 图片链接  
  15.     private String url; // 新闻链接  
  16.     private String pdate_src;// 发布完整时间  
  17.   
  18.         }  

动态添加news_list_item.xml、news_list_item_more_img.xml到LinearLayout

  1. private void showView() {  
  2.   
  3.         for (int i = 0; i < mList.size(); i++) {  
  4.             int type = i % 4;  
  5.             // 初始化news_list_item布局  
  6.             View news_list_item_view = LayoutInflater.from(this).inflate(  
  7.                     R.layout.news_list_item, mLinearLayout, false);  
  8.   
  9.             // 初始化news_list_item__more_img_view布局  
  10.             View news_list_item__more_img_view = LayoutInflater.from(this)  
  11.                     .inflate(R.layout.news_list_item_more_img, mLinearLayout,  
  12.                             false);  
  13.             switch (type) {  
  14.             case 0:  
  15.                 TextView mTitle_More_TV = (TextView) news_list_item__more_img_view  
  16.                         .findViewById(R.id.news_list_item_text_iv);  
  17.                 mTitle_More_TV.setText(mList.get(i).getTitle());  
  18.   
  19.                 mLinearLayout.addView(news_list_item__more_img_view);  
  20.                 break;  
  21.   
  22.             default:  
  23.                 TextView mTitle_TV = (TextView) news_list_item_view  
  24.                         .findViewById(R.id.news_title_tv);  
  25.                 TextView mContent_TV = (TextView) news_list_item_view  
  26.                         .findViewById(R.id.news_overview_tv);  
  27.   
  28.                 mTitle_TV.setText(mList.get(i).getTitle());  
  29.                 mContent_TV.setText(mList.get(i).getContent());  
  30.   
  31.                 mLinearLayout.addView(news_list_item_view);  
  32.                 break;  
  33.             }  
  34.         }  
  35.     }  

》运行效果图

demo2

当前文章价值8.88元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)

你可能感兴趣的文章

来源:每日教程每日一例,深入学习实用技术教程,关注公众号TeachCourse
转载请注明出处: https://www.teachcourse.cn/1180.html ,谢谢支持!

资源分享

分类:Android 标签:,
Android开发之TextView控件设置颜色切换器的问题 Android开发之TextView控件设
Ubuntu系统Python3 Ubuntu系统Python3
自定义弹窗 VS AlertDialog分享弹窗 自定义弹窗 VS AlertDialog分
批处理实现自动刷抖音过程 批处理实现自动刷抖音过程

发表评论

呲牙 憨笑 坏笑 偷笑 色 微笑 抓狂 睡觉 酷 流汗 鼓掌 大哭 可怜 疑问 晕 惊讶 得意 尴尬 发怒 奋斗 衰 骷髅 啤酒 吃饭 礼物 强 弱 握手 OK NO 勾引 拳头 差劲 爱你

表情