ScrollView+LinearLayout 仿Listview 效果
2016-12-17 14:25
169 查看
布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.wsd.sun.myapplication.MainActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ebebeb" android:orientation="vertical" android:scrollbars="none"> <LinearLayout android:id="@+id/get_lottery" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:orientation="vertical" /> </ScrollView> </LinearLayout>
代码
public class MainActivity extends AppCompatActivity {private LinearLayout mLayout;private Context context;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);context=this;mLayout= (LinearLayout) findViewById(R.id.get_lottery);initLottery();}private void initLottery() {mLayout.removeAllViews();final String[] tags = new String[]{"去测试0","去测试1","去测试2","去测试3","去测试4","去测试5","去测试6"};if (tags != null && tags.length > 0) {mLayout.setVisibility(View.VISIBLE);for ( int i = 0; i < tags.length ; i++) {final String name_str = tags[i];View view = LayoutInflater.from(context).inflate(R.layout.list_item_select_lottery, null);TextView nameView = (TextView) view.findViewById(R.id.list_item_lottery);LinearLayout layoutView = (LinearLayout) view.findViewById(R.id.list_item_layout);nameView.setText(name_str);nameView.setTextColor(0xff4d4d4d);layoutView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(context,"点击了:"+name_str,Toast.LENGTH_SHORT).show();}});mLayout.addView(view);}} else {mLayout.setVisibility(View.GONE);}}}demo地址:http://download.csdn.net/detail/wang1897b7/9714057
相关文章推荐
- Ubuntu 14.04 alsa 安装
- C++Primer第五版 第三章习题答案(41~45)
- 使用Intellij加载Spark源代码
- hadoop配置文件hdfs-site.xml
- C# MVC 微信支付教程系列之公众号支付代码
- Android 输入框中原意字符串,十六进制字符串与字节数组
- Laravel框架——分页
- RecyclerView跳转到指定位置的三种方式
- Java设计模式----策略模式
- plupload如何限制上传文件数量,限制只能上传单个文件
- 设置Eclipse的类文件和xml文件代码自动补全
- (OK) Android中集成第三方库的方法和问题
- JavaScript中prototype用法
- 吃糖果(组合数学)
- Emgu OpenFileDialog()打开图像,分别用Image和Mat CvInvoke.Imread 加载
- Cadence元件与走线一起移动的方法
- Linux seq命令笔记
- Emgu Mat方法和Image方法设置一张背景图像
- gdb来debug caffe代码
- easyui combobox的数据value为0时默认选择此项且提交时不会把这个0提交