向ScrollView添加视图会删除边距

时间:2013-08-09 16:00:55

标签: java android

我制作了一个自定义的xml,它具有一个设置了layout_margins的LinearLayout。如果我查看图形布局它显示边距,但在另一个活动中,当我尝试将此视图添加到ScrollView中的LinearLayout时,没有任何边距存在。这是自定义xml代码....

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/run_background"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp">

<TextView
    android:id="@+id/tvRunName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_gravity="center"
    android:layout_marginTop="10dp" />

以下是添加视图的代码....

        for(int x = 0; x < runs.size(); x++){

        inflater = (LayoutInflater)this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.run_layout, null);


        TextView name = (TextView) layout.findViewById(R.id.tvRunName);
        name.setText(runs.get(x).getName());

        llRuns.addView(layout);
    }

我如何将视图分开,就像我想要的那样?

1 个答案:

答案 0 :(得分:1)

在给布局充气时不包括LayoutParams。在膨胀布局时尝试添加第三个参数:

View layout = inflater.inflate(R.layout.run_layout, null, false);
相关问题