记得以前有个Gallery,但是这个以后被Google放弃了,用过的Gallery的人肯定知道不好用,现在更多的是使用ViewPager或者 HorizontalScrollView,今天说下 HorizontalScrollView的简单使用吧,之前项目中都没用到过这个控件,以为是和listview的一样使用,后才才发现HorizontalScrollView没有什么setAdapter()方法,哪怎么绑定一些资源到HorizontalScrollView控件上呢?换个思路,我们可以里面加一个LinearLayout控件,因为LinearLayout它有方向的属性,现在新建一个Android项目/HorizontalDemo,

activity_main.xml

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >


            android:id="@+id/hsv"
        android:layout_width="fill_parent"
        android:layout_height="180dp"
        android:layout_marginTop="20dp"
        >
   android:id="@+id/ll"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   >



MainActivity.java

package com.example.horizontaldemo;




import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends Activity {
private HorizontalScrollView hsv;
private LinearLayout ll;
private int[] mImgIds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hsv = (HorizontalScrollView) findViewById(R.id.hsv);
ll = (LinearLayout) findViewById(R.id.ll);
initData();
initView();
}
private void initView() {

for(int i=0;i
View itemView = getLayoutInflater().inflate(R.layout.item, null);
   ImageView iv = (ImageView) itemView.findViewById(R.id.iv);
   TextView tv_name = (TextView) itemView.findViewById(R.id.tv_name);
   iv.setImageResource(mImgIds[i]);
   tv_name.setText("妹一个");
   ll.addView(itemView);
}
}
private void initData()
{
mImgIds = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,
R.drawable.h, R.drawable.l };
}
}


item.xml

<?xml version="1.0" encoding="utf-8"?>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
            android:id="@+id/iv"
        android:layout_width="90dp"
        android:layout_height="90dp"
        />
   android:id="@+id/tv_name"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   />

效果图:


在这感谢http://blog.csdn.net/lmj623565791/article/details/38140505博客的图片资源

更多相关文章

  1. Android(安卓)Studio 新建项目 只有.idea文件夹
  2. [转]Android(安卓)Test Auto Test Mu…
  3. android开发 软键盘出现后 防止EditText控件遮挡 整体平移UI
  4. Android(安卓)动画机制(三)
  5. Android(安卓)API:自定义ViewGroup
  6. Android实时监控项目第四篇:后台线程发送预览帧视频数据
  7. Android(安卓)Studio gradle sync failed 笔记
  8. android DIY 2
  9. 一步一步学习Android(安卓)TV/盒子开发(三)

随机推荐

  1. what is Android?
  2. 【30篇突击 android】源码统计一
  3. Could not GET 'https://dl.google.com/d
  4. Android(安卓)RelativeLayout中layout_ma
  5. android titlebar 隐藏
  6. RecyclerView的 overScrollMode,scrollbar
  7. 【分享】大量android软件开发资料和实例
  8. 大量android软件开发资料和实例代码
  9. Android常用工具的使用
  10. Android(安卓)Gesture Detector