【问题标题】:android - spacing of items added to linear layout changesandroid - 添加到线性布局更改的项目间距
【发布时间】:2016-03-31 11:28:12
【问题描述】:

我正在以编程方式将图片添加到线性布局。这个线性布局被一个水平滚动视图和列表视图的项目布局的一部分包围。当我将图片与其他视图项内联时,它们正确地彼此相邻:

但是,如果我将水平滚动视图/线性布局移动到其他视图项下,我会得到一些 android 似乎自动执行的奇怪间距:

到目前为止,我已经尝试过相对布局、嵌入式线性布局、更改填充、更改边距、更改 match_parent、fill_parent 和 wrap_content 之间的线性布局的宽度属性,但没有任何改变此间距。它总是一样的。

以下是相关代码:

LinearLayout tmpLL = (LinearLayout) convertView.findViewById(R.id.llUpgrades);

        //remove previous list contents first
        tmpLL.removeAllViews();

        for(int i = 0; i<= tmpUpgradeList.size()-1; i++){

            ImageView tmpIB = new ImageView(getContext());
            Upgrade tmpUpgrade = tmpUpgradeList.get(i);
            Upgrade.setUpgradePic(tmpIB, tmpUpgrade, tmpUpgrade.Title()==null);
            tmpIB.setTag(position + ":" + i);
            tmpIB.setPadding(5, 0, 0, 0);
            tmpIB.setMaxWidth(50);

            tmpLL.addView(tmpIB);

            tmpIB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                      String[] split = ((String) v.getTag()).split(":");
                     runUpgradePopup(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
                }
            });

            tmpIB.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                     String[] split = ((String) v.getTag()).split(":");
                     clearUpgrade(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
                     return true;
                }
            });

        }

导致错误的布局。另一种布局正确地将卡片彼此相邻,但我所不同的是它是线性布局并删除了相关的相对放置调用:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">


        <Button
            android:layout_width="100dp"
            android:layout_height="60dp"
            android:id="@+id/btnFRemoveShip"
            android:text="Remove"/>

        <ImageView
            android:id="@+id/ivFRowShipIcon"
            android:layout_height="60dp"
            android:layout_width="75dp"
            android:src="@android:drawable/ic_delete"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/btnFRemoveShip"/>


        <TextView
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_width="wrap_content"
            android:id="@+id/tvFRowShipTitle"
            android:text="error"
            android:textSize="20dp"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/ivFRowShipIcon"/>



        <HorizontalScrollView
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_marginTop="5dp"
            android:layout_below="@+id/btnFRemoveShip">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/llUpgrades">
            </LinearLayout>
        </HorizontalScrollView>


</RelativeLayout>

非常感谢任何帮助!

【问题讨论】:

  • 尝试使用 wrap_content 进行滚动和线性布局。
  • nerp,尝试了滚动/换行、线性/填充和滚动/换行、线性/换行但结果完全相同

标签: android android-layout android-linearlayout


【解决方案1】:

花了几天的时间改写问题,但终于找到了答案。 必须在将图像添加到布局之前添加此行。

tmpIB.setAdjustViewBounds(true);

线程在这里:found answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    相关资源
    最近更新 更多