android自定义横竖双向滚动,如何创建自定义相对布局的滚动列表,而不是在Android Studio中相互堆叠?...

我想创建一个滚动视图列表,它将显示“框”或更精确的自定义布局(我使用扩展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);

}

}

}

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">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/scrollViewRestaurants"

android:layout_below="@+id/txtWelcomeUser"

android:layout_alignParentStart="true">

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">

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" />

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" />

>

+0

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

+0

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值