Android动态加载布局

2019-12-10 19:11:25王旭
易采站长站为您分析Android动态加载布局,感兴趣的小伙伴们可以参考一下  

ListView我们一直都在用,只不过当Adapter中的内容比较多的时候我们有时候没办法去设置一些组件,举个例子:

Android动态加载布局

可以看到京东的故事里面的这样一个布局,这个布局可以说是我目前见到的内容比较多的了,它的每一项都包含头像、姓名、分类、内容、图片、喜欢、评论、分享以及喜欢的头像。分析了一下布局之后我们不难发现,除了喜欢头像这部分,其余的都很好实现。

那么下面着重说一下这个头像这部分怎么实现?

第一种方案:我们可以用GridView来实现,GridView和ListView的用法是一样的,俗称九宫格排列,那么我们可以将GridView的一行排列九张图片来显示这些头像,只不过ListView嵌套着GridView稍微的有些麻烦,自己做了一个,感觉不太好用,有想要ListView嵌套GridView的可以私密我。

第二种方案:就是本篇文章所讲的动态加载布局了:

很简单,我们在ListView中定义一个LinerLayout线性布局,用来存放这些头像,先看一下布局吧:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
 
 <LinearLayout 
   android:padding="@dimen/small_space" 
   android:orientation="horizontal" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content"> 
 
  <com.view.RoundedImageView 
    android:id="@+id/iv_myspace_usericon" 
    android:src="@drawable/usericon" 
    android:layout_width="50dp" 
    android:layout_height="50dp"/> 
  <TextView 
    android:id="@+id/tv_myspace_username" 
    android:layout_marginLeft="@dimen/middle_space" 
    android:layout_gravity="center_vertical" 
    android:text="王某某" 
    android:textSize="@dimen/small_textSize" 
    android:layout_weight="1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
  <TextView 
    android:id="@+id/tv_myspace_time" 
    android:textColor="@color/normal_bar_futext_color" 
    android:textSize="@dimen/smallest_textSize" 
    android:layout_gravity="center_vertical" 
    android:text="2015-8-26 17.46" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
 
 </LinearLayout> 
 <TextView 
   android:id="@+id/tv_myspace_content" 
   android:paddingRight="@dimen/middle_space" 
   android:paddingLeft="@dimen/middle_space" 
   android:paddingBottom="@dimen/middle_space" 
   android:text="受到了房间啊了会计分录会计法舰队司令减肥;立刻受到杰弗里斯到付款;老是觉得烦;老卡机的说法;就是看到的就发了卡就" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content"/> 
 <ImageView 
   android:id="@+id/iv_myspace_image" 
   android:scaleType="fitXY" 
   android:src="@drawable/moren" 
   android:paddingRight="@dimen/middle_space" 
   android:paddingLeft="@dimen/middle_space" 
   android:layout_width="match_parent" 
   android:layout_height="140dp"/> 
 <LinearLayout 
   android:id="@+id/ll_myspace_reply_icons" 
   android:paddingTop="@dimen/small_space" 
   android:paddingRight="@dimen/middle_space" 
   android:paddingLeft="@dimen/middle_space" 
   android:orientation="horizontal" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content"> 
 </LinearLayout> 
 <LinearLayout 
   android:layout_marginTop="@dimen/small_space" 
   android:paddingRight="@dimen/middle_space" 
   android:paddingLeft="@dimen/middle_space" 
   android:orientation="horizontal" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content"> 
  <ImageView 
    android:id="@+id/iv_myspace_like" 
    android:src="@drawable/zan_icon" 
    android:layout_width="20dp" 
    android:layout_height="20dp"/> 
   <ImageView 
     android:visibility="gone" 
     android:id="@+id/iv_myspace_liked" 
     android:src="@drawable/wozaixianchang_dianzanxi" 
     android:layout_width="20dp" 
     android:layout_height="20dp"/> 
  <TextView 
    android:id="@+id/tv_myspace_zan_count" 
    android:layout_gravity="center_vertical" 
    android:text="0" 
    android:textColor="@color/normal_bar_futext_color" 
    android:layout_marginLeft="@dimen/small_space" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
  <ImageView 
    android:id="@+id/iv_myspace_comment" 
    android:layout_marginLeft="@dimen/middle_space" 
    android:src="@drawable/pinglun_icon" 
    android:layout_width="20dp" 
    android:layout_height="20dp"/> 
  <TextView 
    android:id="@+id/tv_myspace_pinglun_count" 
    android:layout_gravity="center_vertical" 
    android:text="0" 
    android:textColor="@color/normal_bar_futext_color" 
    android:layout_marginLeft="@dimen/small_space" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
 
 </LinearLayout> 
 
</LinearLayout> 


<LinearLayout 
   android:id="@+id/ll_myspace_reply_icons" 
   android:paddingTop="@dimen/small_space" 
   android:paddingRight="@dimen/middle_space" 
   android:paddingLeft="@dimen/middle_space" 
   android:orientation="horizontal" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content"> 
 </LinearLayout>