Java 如何在Android Studio中创建自定义相对布局的滚动列表,而不是彼此堆叠?

Java 如何在Android Studio中创建自定义相对布局的滚动列表,而不是彼此堆叠?,java,android,layout,scrollview,android-relativelayout,Java,Android,Layout,Scrollview,Android Relativelayout,我想创建一个滚动视图列表,该列表将显示“框”或更精确的定制布局(我使用的是一个扩展RelativeLayout的定制类-基本上显示不同的信息,有些是静态的,如地点的工作时间,有些是动态更改的,将从服务器中提取) 虽然我遇到了一个问题-我(为了看看我的解决方案是否有效)创建了5个框并将它们添加到滚动视图列表中,但它们似乎是相互堆叠的。在不手动调整其位置坐标的情况下,如何正确地使它们一个接一个地显示?我使用addView()就是为了这个目的,但它不能像我预期的那样工作,或者我使用得很糟糕。如果您知道

我想创建一个滚动视图列表,该列表将显示“框”或更精确的定制布局(我使用的是一个扩展RelativeLayout的定制类-基本上显示不同的信息,有些是静态的,如地点的工作时间,有些是动态更改的,将从服务器中提取)

虽然我遇到了一个问题-我(为了看看我的解决方案是否有效)创建了5个框并将它们添加到滚动视图列表中,但它们似乎是相互堆叠的。在不手动调整其位置坐标的情况下,如何正确地使它们一个接一个地显示?我使用addView()就是为了这个目的,但它不能像我预期的那样工作,或者我使用得很糟糕。如果您知道答案,请简要描述应该如何做到这一点

提前谢谢

编辑,下面是代码:

public class RestaurantBox extends RelativeLayout {
    RestaurantBox (Context context){
        super(context);
        this.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
        TextView restaurantName = new TextView(context);
        restaurantName.setText("Test Restaurant");
        RelativeLayout.LayoutParams restNameParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.addView(restaurantName, restNameParams);
        restaurantName.setTypeface(null, Typeface.BOLD);
        TextView freeSpots = new TextView(context);
        freeSpots.setText("15/20");
        RelativeLayout.LayoutParams freeSpotParams =  new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        freeSpotParams.topMargin = restNameParams.bottomMargin + 50;
        this.addView(freeSpots, freeSpotParams);
        TextView book = new TextView(this.getContext());
        RelativeLayout.LayoutParams bookParams =  new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        book.setText("Book");
        bookParams.setMargins(0,freeSpotParams.bottomMargin + 100,0,0);
        this.addView(book, bookParams);
    }
}


public class BrowseRestaurants extends AppCompatActivity {
    int restaurantCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_restaurants);
        Intent intent = getIntent();
        String login = intent.getStringExtra("LOGIN");
        TextView text = (TextView) findViewById(R.id.txtWelcomeUser);
        text.setText("Welcome, " + login);
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
        RestaurantBox initRBox = new RestaurantBox(this);
        initRBox.setTop(0);
        initRBox.setBottom(300);
        relativeLayout.addView(initRBox);
        for(int i=1;i<5;i++){
            final View view = relativeLayout.getChildAt(i-1);
            RestaurantBox restaurantBox = new RestaurantBox(this);
            restaurantBox.setTop(view.getBottom() + 50);
            restaurantBox.setBottom(restaurantBox.getTop() + 300);
            relativeLayout.addView(restaurantBox);
        }
    }
}
<!-- activity_browse_restaurants.xml-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:fillViewport="true"
    android:orientation="vertical"
    tools:context="com.konrad.rezerwacje1.BrowseRestaurants">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollViewRestaurants"
        android:layout_below="@+id/txtWelcomeUser"
        android:layout_alignParentStart="true">
        <RelativeLayout
            android:id="@+id/relLayoutRestaurants"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="65dp"
            android:orientation="vertical">
        </RelativeLayout>
    </ScrollView>
    <TextView
        android:id="@+id/txtLogout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Logout"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="18sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />
    <TextView
        android:id="@+id/txtWelcomeUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="@android:color/holo_blue_dark"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="9dp" />
    </RelativeLayout
公共类RestaurantBox扩展了RelativeLayout{
RestaurantBox(上下文){
超级(上下文);
this.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
TextView restaurantName=新的TextView(上下文);
restaurantName.setText(“测试餐厅”);
RelativeLayout.LayoutParams restNameParams=新的RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
this.addView(restaurantName、restNameParams);
restaurantName.setTypeface(null,Typeface.BOLD);
TextView freeSpots=新的TextView(上下文);
freeSpots.setText(“15/20”);
RelativeLayout.LayoutParams freeSpotParams=新的RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
freeSpotParams.topMargin=restNameParams.bottomMargin+50;
this.addView(freeSpots,freeSpotParams);
TextView book=newtextView(this.getContext());
RelativeLayout.LayoutParams bookParams=新的RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
book.setText(“book”);
bookParams.setMargins(0,freeSpotParams.bottomMargin+100,0,0);
this.addView(book,bookParams);
}
}
公共类BrowserEstaturants扩展应用程序活动{
国际餐厅账户;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u browse\u restaurants);
Intent=getIntent();
String login=intent.getStringExtra(“登录”);
TextView text=(TextView)findViewById(R.id.txtwlcomeuser);
setText(“欢迎,”+登录);
RelativeLayout RelativeLayout=(RelativeLayout)findViewById(R.id.relLayoutRestaurants);
RestaurantBox initRBox=新的RestaurantBox(此);
initRBox.setTop(0);
initRBox.setBottom(300);
relativeLayout.addView(initRBox);

对于(int i=1;i您正在RelativeLayout中添加视图,因此视图彼此堆叠,因此将其更改为activity\u browse\u restaurants.xml中的LinearLayout

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollViewRestaurants"
        android:layout_below="@+id/txtWelcomeUser"
        android:layout_alignParentStart="true">
        <LinearLayout
            android:id="@+id/relLayoutRestaurants"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="65dp"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>


如果您想将变量名更改为u,则可以使用其他方法,如果有任何问题,请告诉我

在您添加的位置显示您的xml和代码view@Pavan代码被添加了!那么视图u在添加RellayoutRestarants时,您希望它垂直堆叠吗?
 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.relLayoutRestaurants);