ScrollView不太繁琐,但是他的布局文件有时候要往Scrollview的子布局里添加好多TextView或者其他等等,很是麻烦.
那我今天就来用下简单的介绍一下scrollview
首先Xml布局可以看出就一个scrollview和一个LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ScrollView android:scrollbars="none" android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/linear" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> </ScrollView> </LinearLayout>
然后重点来了,他要显示的哪去了呢,不要急,下面就展示出来了.
private String[] str=new String[]{"首页","今日头条","视频","我的"}; private LinearLayout linearLayout; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayout= (LinearLayout) findViewById(R.id.linear); for (int i = 0; i <str.length ; i++) { View inflste=getLayoutInflater().inflate(R.layout.film_itrm,null); ImageView tv_image= (ImageView) inflste.findViewById(R.id.imageView); TextView tv_text=(TextView) inflste.findViewById(R.id.textView); tv_image.setImageResource(R.mipmap.ic_launcher); tv_text.setText(str[i]); linearLayout.addView(inflste); } }还有一个Xml布局也是配置的布局很是简单对就是这些,就是这点吧他搞定了,他即简化了Xml布局又很是方便啊<ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" />