简单的方法实现ScrollView

本文介绍了一种使用ScrollView简化Android应用布局的方法。通过示例代码展示了如何动态添加TextView和其他视图到LinearLayout中,使得布局更加灵活且易于维护。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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布局也是配置的布局很是简单
<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" />

对就是这些,就是这点吧他搞定了,他即简化了Xml布局又很是方便啊
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值