Android 我如何通过编程方式创建一个滚动视图,其中包含这样的相对性对话框?

Android 我如何通过编程方式创建一个滚动视图,其中包含这样的相对性对话框?,android,scrollview,Android,Scrollview,我正在尝试创建一个包含滚动视图的活动。在ScrollView中,框有点像一个按钮,只比父视图的宽度小一点,垂直间隔稍长。因为框上的几个区域会有两个不同大小的文本,所以我想我需要将相对值去掉。我会发布一张照片,但不能与我的声誉点,所以希望这是有意义的 我尝试为活动创建一个XML,然后通过编程添加RelativeView。我让它工作了,有点,但是我在盒子的间距上有问题 我的测试XML如下所示 <LinearLayout android:layout_width="match_parent

我正在尝试创建一个包含滚动视图的活动。在ScrollView中,框有点像一个按钮,只比父视图的宽度小一点,垂直间隔稍长。因为框上的几个区域会有两个不同大小的文本,所以我想我需要将相对值去掉。我会发布一张照片,但不能与我的声誉点,所以希望这是有意义的

我尝试为活动创建一个XML,然后通过编程添加RelativeView。我让它工作了,有点,但是我在盒子的间距上有问题

我的测试XML如下所示

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   xmlns:android="http://schemas.android.com/apk/res/android">

   <!-- scrolling section -->
   <ScrollView
      android:layout_width="fill_parent"
      android:paddingTop="5dp"
      android:layout_height="0dp"
      android:layout_weight="1.30"
      >

       <LinearLayout
        android:id="@+id/llScroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
       </LinearLayout>
    </ScrollView>
 </LinearLayout>
public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test);

    LinearLayout llAccts=(LinearLayout)findViewById(R.id.llScroll);

    for (int i=1;i<20;i++) {
        RelativeLayout rlView = new RelativeLayout(this);
        rlView.setBackgroundColor(Color.BLUE);

        TextView test=new TextView(this);
        test.setText("Test #"+i);
        test.setTextColor(Color.WHITE);
        rlView.addView(test);

        llAccts.addView(rlView,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }
}

}
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- header: non-scrolling -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UPDATE ALL"
            android:id="@+id/btnUpdate" />
    </RelativeLayout>

    <!-- scrolling bottom pane -->
    <ScrollView
        android:layout_width="fill_parent"
        android:paddingTop="5dp"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        >

        <LinearLayout
            android:id="@+id/llGroups"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            // individual groups start here - this section will be controlled by a database

            // #1 group
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:background="@color/blue"
                android:padding="5dp"
                android:clickable="true"
                android:id="@+id/grp1"
                >

                <TextView
                    android:id="@+id/text1"
                    android:text="Item #1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:id="@+id/leftext1"
                    android:text="123.00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:textSize="30dp" />
            </RelativeLayout>

            // #2 group
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@color/blue"
                android:padding="5dp"
                android:clickable="true"
                android:id="@+id/grp2"
                >

                <TextView
                    android:id="@+id/text2"
                    android:text="Item #2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/leftext2"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:id="@+id/leftext2"
                    android:text="456.00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:textSize="30dp"
                    />
            </RelativeLayout>

        </LinearLayout>
    </ScrollView>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- header: non-scrolling -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UPDATE ALL"
            android:id="@+id/btnUpdate" />
    </RelativeLayout>

    <!-- scrolling bottom pane -->
    <ScrollView
        android:layout_width="fill_parent"
        android:paddingTop="5dp"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        >

        <LinearLayout
            android:id="@+id/llGroups"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
        </LinearLayout>
    </ScrollView>
</LinearLayout>
public class Main2Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);


        LinearLayout llGroups=(LinearLayout)findViewById(R.id.llGroups);

        for (int i=1;i<20;i++) {
            RelativeLayout rlView = new RelativeLayout(this);
            rlView.setBackgroundColor(Color.BLUE);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            rlp.setMargins(10,10,10,0);

            TextView test=new TextView(this);
            test.setText("Test #"+i);
            test.setTextColor(Color.WHITE);
            rlView.addView(test);

            // other textviews will go here

            llGroups.addView(rlView,rlp);
        }
    }
}
就在背景色之后。所有看起来要做的就是填充文本。。。不是相对论者

有什么建议吗?没有,怎么做

谢谢

===========

新资料: 下面是在遵循一些建议并尝试错误后的一些附加信息。我还可以发布一张照片,以便于参考

===========

我试图通过编程实现的是这样的东西。

我使用直接的XML实现了这个示例,看起来像这样

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   xmlns:android="http://schemas.android.com/apk/res/android">

   <!-- scrolling section -->
   <ScrollView
      android:layout_width="fill_parent"
      android:paddingTop="5dp"
      android:layout_height="0dp"
      android:layout_weight="1.30"
      >

       <LinearLayout
        android:id="@+id/llScroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
       </LinearLayout>
    </ScrollView>
 </LinearLayout>
public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test);

    LinearLayout llAccts=(LinearLayout)findViewById(R.id.llScroll);

    for (int i=1;i<20;i++) {
        RelativeLayout rlView = new RelativeLayout(this);
        rlView.setBackgroundColor(Color.BLUE);

        TextView test=new TextView(this);
        test.setText("Test #"+i);
        test.setTextColor(Color.WHITE);
        rlView.addView(test);

        llAccts.addView(rlView,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }
}

}
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- header: non-scrolling -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UPDATE ALL"
            android:id="@+id/btnUpdate" />
    </RelativeLayout>

    <!-- scrolling bottom pane -->
    <ScrollView
        android:layout_width="fill_parent"
        android:paddingTop="5dp"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        >

        <LinearLayout
            android:id="@+id/llGroups"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            // individual groups start here - this section will be controlled by a database

            // #1 group
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:background="@color/blue"
                android:padding="5dp"
                android:clickable="true"
                android:id="@+id/grp1"
                >

                <TextView
                    android:id="@+id/text1"
                    android:text="Item #1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:id="@+id/leftext1"
                    android:text="123.00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:textSize="30dp" />
            </RelativeLayout>

            // #2 group
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@color/blue"
                android:padding="5dp"
                android:clickable="true"
                android:id="@+id/grp2"
                >

                <TextView
                    android:id="@+id/text2"
                    android:text="Item #2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/leftext2"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:id="@+id/leftext2"
                    android:text="456.00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:textSize="30dp"
                    />
            </RelativeLayout>

        </LinearLayout>
    </ScrollView>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- header: non-scrolling -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UPDATE ALL"
            android:id="@+id/btnUpdate" />
    </RelativeLayout>

    <!-- scrolling bottom pane -->
    <ScrollView
        android:layout_width="fill_parent"
        android:paddingTop="5dp"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        >

        <LinearLayout
            android:id="@+id/llGroups"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
        </LinearLayout>
    </ScrollView>
</LinearLayout>
public class Main2Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);


        LinearLayout llGroups=(LinearLayout)findViewById(R.id.llGroups);

        for (int i=1;i<20;i++) {
            RelativeLayout rlView = new RelativeLayout(this);
            rlView.setBackgroundColor(Color.BLUE);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            rlp.setMargins(10,10,10,0);

            TextView test=new TextView(this);
            test.setText("Test #"+i);
            test.setTextColor(Color.WHITE);
            rlView.addView(test);

            // other textviews will go here

            llGroups.addView(rlView,rlp);
        }
    }
}
我将其分解并创建了一个XML文件和活动,该文件和活动将以编程方式创建中间组

修改后的XML活动\u main2如下所示

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   xmlns:android="http://schemas.android.com/apk/res/android">

   <!-- scrolling section -->
   <ScrollView
      android:layout_width="fill_parent"
      android:paddingTop="5dp"
      android:layout_height="0dp"
      android:layout_weight="1.30"
      >

       <LinearLayout
        android:id="@+id/llScroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
       </LinearLayout>
    </ScrollView>
 </LinearLayout>
public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test);

    LinearLayout llAccts=(LinearLayout)findViewById(R.id.llScroll);

    for (int i=1;i<20;i++) {
        RelativeLayout rlView = new RelativeLayout(this);
        rlView.setBackgroundColor(Color.BLUE);

        TextView test=new TextView(this);
        test.setText("Test #"+i);
        test.setTextColor(Color.WHITE);
        rlView.addView(test);

        llAccts.addView(rlView,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }
}

}
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- header: non-scrolling -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UPDATE ALL"
            android:id="@+id/btnUpdate" />
    </RelativeLayout>

    <!-- scrolling bottom pane -->
    <ScrollView
        android:layout_width="fill_parent"
        android:paddingTop="5dp"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        >

        <LinearLayout
            android:id="@+id/llGroups"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            // individual groups start here - this section will be controlled by a database

            // #1 group
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:background="@color/blue"
                android:padding="5dp"
                android:clickable="true"
                android:id="@+id/grp1"
                >

                <TextView
                    android:id="@+id/text1"
                    android:text="Item #1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:id="@+id/leftext1"
                    android:text="123.00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:textSize="30dp" />
            </RelativeLayout>

            // #2 group
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@color/blue"
                android:padding="5dp"
                android:clickable="true"
                android:id="@+id/grp2"
                >

                <TextView
                    android:id="@+id/text2"
                    android:text="Item #2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/leftext2"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:id="@+id/leftext2"
                    android:text="456.00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:textSize="30dp"
                    />
            </RelativeLayout>

        </LinearLayout>
    </ScrollView>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- header: non-scrolling -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UPDATE ALL"
            android:id="@+id/btnUpdate" />
    </RelativeLayout>

    <!-- scrolling bottom pane -->
    <ScrollView
        android:layout_width="fill_parent"
        android:paddingTop="5dp"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        >

        <LinearLayout
            android:id="@+id/llGroups"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
        </LinearLayout>
    </ScrollView>
</LinearLayout>
public class Main2Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);


        LinearLayout llGroups=(LinearLayout)findViewById(R.id.llGroups);

        for (int i=1;i<20;i++) {
            RelativeLayout rlView = new RelativeLayout(this);
            rlView.setBackgroundColor(Color.BLUE);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            rlp.setMargins(10,10,10,0);

            TextView test=new TextView(this);
            test.setText("Test #"+i);
            test.setTextColor(Color.WHITE);
            rlView.addView(test);

            // other textviews will go here

            llGroups.addView(rlView,rlp);
        }
    }
}
我开始重写我的活动代码来测试一些建议。现在看起来是这样的

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   xmlns:android="http://schemas.android.com/apk/res/android">

   <!-- scrolling section -->
   <ScrollView
      android:layout_width="fill_parent"
      android:paddingTop="5dp"
      android:layout_height="0dp"
      android:layout_weight="1.30"
      >

       <LinearLayout
        android:id="@+id/llScroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
       </LinearLayout>
    </ScrollView>
 </LinearLayout>
public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test);

    LinearLayout llAccts=(LinearLayout)findViewById(R.id.llScroll);

    for (int i=1;i<20;i++) {
        RelativeLayout rlView = new RelativeLayout(this);
        rlView.setBackgroundColor(Color.BLUE);

        TextView test=new TextView(this);
        test.setText("Test #"+i);
        test.setTextColor(Color.WHITE);
        rlView.addView(test);

        llAccts.addView(rlView,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }
}

}
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- header: non-scrolling -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UPDATE ALL"
            android:id="@+id/btnUpdate" />
    </RelativeLayout>

    <!-- scrolling bottom pane -->
    <ScrollView
        android:layout_width="fill_parent"
        android:paddingTop="5dp"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        >

        <LinearLayout
            android:id="@+id/llGroups"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            // individual groups start here - this section will be controlled by a database

            // #1 group
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:background="@color/blue"
                android:padding="5dp"
                android:clickable="true"
                android:id="@+id/grp1"
                >

                <TextView
                    android:id="@+id/text1"
                    android:text="Item #1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:id="@+id/leftext1"
                    android:text="123.00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:textSize="30dp" />
            </RelativeLayout>

            // #2 group
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@color/blue"
                android:padding="5dp"
                android:clickable="true"
                android:id="@+id/grp2"
                >

                <TextView
                    android:id="@+id/text2"
                    android:text="Item #2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/leftext2"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:id="@+id/leftext2"
                    android:text="456.00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:textSize="30dp"
                    />
            </RelativeLayout>

        </LinearLayout>
    </ScrollView>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- header: non-scrolling -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UPDATE ALL"
            android:id="@+id/btnUpdate" />
    </RelativeLayout>

    <!-- scrolling bottom pane -->
    <ScrollView
        android:layout_width="fill_parent"
        android:paddingTop="5dp"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        >

        <LinearLayout
            android:id="@+id/llGroups"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
        </LinearLayout>
    </ScrollView>
</LinearLayout>
public class Main2Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);


        LinearLayout llGroups=(LinearLayout)findViewById(R.id.llGroups);

        for (int i=1;i<20;i++) {
            RelativeLayout rlView = new RelativeLayout(this);
            rlView.setBackgroundColor(Color.BLUE);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            rlp.setMargins(10,10,10,0);

            TextView test=new TextView(this);
            test.setText("Test #"+i);
            test.setTextColor(Color.WHITE);
            rlView.addView(test);

            // other textviews will go here

            llGroups.addView(rlView,rlp);
        }
    }
}
这将编译并运行,但setMargins不起作用。我的屏幕顶部有一个应该的“全部更新”按钮和19个蓝色背景的文本,但它们是全宽的,相互接触,所以是纯蓝色背景

我接近了,但不确定如何为rlView relativeview而不是textview正确设置边距

希望这有助于解释我现在看到的

解决方案

经过建议和一些尝试和错误。。。下面是我必须做的事情,以实现所需的布局,以防其他人感兴趣

    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    RelativeLayout.LayoutParams tvp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    RelativeLayout.LayoutParams tvp2 = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


    LinearLayout mTvContainer = (LinearLayout) findViewById(R.id.llAccts);

    for (int i = 0; i < 20; i++) {
        LinearLayout ll = new LinearLayout(this);

        ll.setBackgroundColor(Color.BLUE);

        RelativeLayout rl = new RelativeLayout(this);

        TextView tv = new TextView(this);
        tv.setTextColor(Color.WHITE);
        tv.setPadding(5, 5, 5, 5);

        tv.setText("Test #" + i);
        tvp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        TextView tv2 = new TextView(this);
        tv2.setTextColor(Color.WHITE);
        tv2.setPadding(5, 5, 5, 5);
        tv2.setText(String.valueOf(i));
        tv2.setTextSize(30);
        tvp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        rl.addView(tv, tvp);
        rl.addView(tv2,tvp2);

        ll.addView(rl);

        llp.setMargins(32,16,32,16);

        mTvContainer.addView(ll,llp);
    }
}
对于For中的每个条目,我必须使用LINEARLAYOUT来正确设置边距,在该LINEARLAYOUT中,我必须使用RELATIVELAYOUT来使用addRules来获得文本视图的正确位置


如果有更简单的方法,请告诉我。谢谢大家的帮助

您使用了太多需要的嵌套布局,下面是精简版

xml:

活动代码:

mTvContainer = (LinearLayout) findViewById(R.id.ll_main);
int p = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);

for (int i = 0; i < 20; i++) {
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);

    TextView tv = new TextView(this);
    tv.setBackgroundColor(Color.BLUE);
    tv.setTextColor(Color.WHITE);
    tv.setPadding(p, p / 2, p, p / 2);
    tv.setText("TextView " + i);

    TextView tv2 = new TextView(this);
    tv2.setBackgroundColor(Color.RED);
    tv2.setTextColor(Color.WHITE);
    tv2.setPadding(p, p / 2, p, p / 2);
    tv2.setText("TextView " + i);

    ll.addView(tv);
    ll.addView(tv2);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(p, p / 2, p, p / 2);
    mTvContainer.addView(ll, params);
}

将TextView置于RelativeLayout中有什么具体原因吗?我认为在一个布局中只有一个视图实际上是无用的。另外,在添加ito RelativeLayout时,您没有在TextView中放置任何布局参数。请尝试向RelativeLayout添加页边距。为什么不使用recyclerview@Abdullah,将会有不止一个文本视图,我只是在这次测试中把它放进去。实际上会有3到4个不同大小、颜色和位置的。@Abdullah。。。我编辑了我的帖子,添加了一张图片和一些额外的代码,看看我是否能澄清我试图实现的目标。我会试试这个,但我的问题是看代码。。。填充是围绕文本视图而不是布局的,不是吗?布局将在不同的位置有几个字体大小和颜色的文本视图,并且希望整个布局像一个大按钮一样为蓝色。在这种情况下,向包含多个文本视图的布局添加边距就可以了。因此,您可以使用类似的方法,如上文所述,在将TextView添加到版面时调用params.setMargins,但将该页边距添加到包含这些TextView的版面中。页边距在让它们与RelativeLayout一起使用时仍然存在问题。我编辑了我的帖子,添加了一些新代码和图片。有没有关于让边距发挥作用的建议?更新了将2个文本视图包装在一个线性布局中的代码。利润很好,谢谢你。。您的代码除了一个修复外,其他都可以正常工作。。。必须在第一行的MTV容器之前添加线性布局。但我注意到你把相对位置改成了线性布局。当我这么做的时候,它确实起作用了。但是我需要能够把文本视图放在盒子里的不同位置,看看图片。为此,我不需要一个相对人,这样我就可以把addRules放进去了吗?如果我将LL声明更改为RelativeLayout并去掉方向,它将部分工作。如果我将LayoutParams更改为RelativeLayouts,那就是我得到的是纯蓝色。。。没有利润。