1. layout_main.xml添加空内容的linearlayout
<HorizontalScrollView
android:id="@+id/listimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
2. 新建fragmentlistimage.xml文件,存放添加的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/first"
android:layout_width="180dp"
android:layout_height="100dp"
android:src="@drawable/blue_ocean"/>
<ImageView
android:id="@+id/first1"
android:layout_width="180dp"
android:layout_height="100dp"
android:src="@drawable/blue_ocean01"/>
<ImageView
android:id="@+id/first2"
android:layout_width="180dp"
android:layout_height="100dp"
android:src="@drawable/blue_ocean05"/>
<ImageView
android:id="@+id/first3"
android:layout_width="180dp"
android:layout_height="100dp"
android:src="@drawable/blue_ocean06"/>
<ImageView
android:id="@+id/first4"
android:layout_width="180dp"
android:layout_height="100dp"
android:src="@drawable/blue_ocean07"/>
<ImageView
android:id="@+id/first5"
android:layout_width="180dp"
android:layout_height="100dp"
android:src="@drawable/blue_ocean09"/>
</LinearLayout>
3. 新建java类文件,将fragmentlistimage.xml的视图返回
public class fragmentlistimage extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragmentlistimage,container,false);
}
}
4. 在MainActivity类文件中添加布局
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().add(R.id.listimage,new fragmentlistimage()).commit();
}
}