HorizontalScrollView动态添加item居中问题

本文解决HorizontalScrollView中子View显示不全及居中问题,通过调整XML布局属性,确保所有子View均可正常显示,并实现居中效果。

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

今天在项目中使用HorizontalScrollView来实现动态添加子控件并且 居中的时候遇到了两个问题.
1 HorizontalScrollView的使用布局如下

     <HorizontalScrollView
                    android:id="@+id/scrollView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="@dimen/bottom_img_left"
                    android:paddingRight="@dimen/title_bar_add_right"
                    android:layout_marginTop="@dimen/shop_detail_imgs_top"
                    android:layout_marginBottom="@dimen/shop_detail_imgs_top"
                    android:scrollbars="none">
                    <LinearLayout
                    android:id="@+id/activity_createvisit_gallery"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_gravity="center_horizontal"
                />
                </HorizontalScrollView>

然后我在代码中动态的去给这个LinearLayout添加子控件并且居中,但此时会出现一个问题,当你添加的子view超过3个的时候会出现子viwe显示不完全的问题,比如你添加了4个view缺只能显示3个,添加6个只能显示4个.

2在网上查询了子view显示不全的解决方法.只在xml里面定义一个HorizontalScrollView,然后在代码中新建LinearLayout,然后再动态的去给这LinearLayout添加子view,经测试确实可以解决子view显示不全的问题,但是有时候会碰到这样的需求而,要求子view从中间开始显示,然后我在这种方法里找不到无法居中的实现方法 于是参考了另一篇文章.

解决方法如下:
修改你的xml布局即可 将我上述的xml修改如下即可

        <HorizontalScrollView
                    android:id="@+id/scrollView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="@dimen/shop_detail_imgs_top"
                    android:layout_marginTop="@dimen/shop_detail_imgs_top"
                    android:paddingLeft="@dimen/bottom_img_left"
                    android:paddingRight="@dimen/title_bar_add_right"
                    android:scrollbars="none">
                    <LinearLayout
                        android:id="@+id/activity_createvisit_gallery"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                    />
                </HorizontalScrollView>

亲测有效,希望能对各位有所帮助

private void addTableRow(TableLayout table, Object[] rowData) { TableRow row = new TableRow(requireContext()); Log.d(“log”,rowData[0].toString()); for (Object data : rowData) { TextView textView = new TextView(requireContext()); textView.setText(String.valueOf(data)); textView.setPadding(8, 12, 8, 12); row.addView(textView); } table.addView(row);<?xml version=“1.0” encoding=“utf-8”?> <androidx.core.widget.NestedScrollView xmlns:android=“http://schemas.android.com/apk/res/android” android:layout_width=“match_parent” android:layout_height=“match_parent”> <!-- 双滚动容器实现 --> <HorizontalScrollView android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:fillViewport=“true”> <TableLayout android:id="@+id/orderTable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="*" android:shrinkColumns="*" android:paddingEnd="16dp"> <!-- 表格标题行 --> <TableRow android:background="?attr/colorPrimary" android:padding="8dp"> <TextView android:text="订单号" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="产品编号" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="产品数量" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="组件名" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="板材信息" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="板材/组件" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="订购数量" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> </TableRow> </TableLayout> </HorizontalScrollView> </androidx.core.widget.NestedScrollView> }如何解决载入数据后表格变形,单行可以变形但不能上下变形上下距离一个字符,横向可以超出页面并页面可以上下左右滚动
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值