2017-05-30 27 views
0

我想创建一个滚动视图列表,它将显示“框”或更精确的自定义布局(我使用扩展RelativeLayout的自定义类 - 基本显示不同的信息片断,有些静态像地方的工作时间有些会动态变化,并会从服务器中取出)。如何创建自定义相对布局的滚动列表,而不是在Android Studio中相互堆叠?

尽管我遇到了一个问题 - 我(为了查看我的解决方案是否有效)创建了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 

>

+0

显示你的xml和代码在哪里你添加视图 – Pavan

+0

@Pavan代码被添加! – cooktheprogrammer

+0

所以你认为你添加relLayoutRestaurants你想要verticaly堆叠? – Pavan

回答

1

您在添加视图的RelativeLayout所以查看activity_browse_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> 

现在根据更改在BrowseRestaurants.class

更换

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants); 

用,如果你想与变量改变其高达ü

LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.relLayoutRestaurants); 

否则将被罚款,让我知道是否有问题

+1

它的工作,谢谢一吨! – cooktheprogrammer